>  기사  >  백엔드 개발  >  C# 끝으로()

C# 끝으로()

PHPz
PHPz원래의
2024-09-03 15:17:36651검색

주어진 문자열이 문자열의 끝과 일치하는지 확인하는 데 사용되는 메서드를 C#에서는 EndsWith() 메서드라고 합니다. 주어진 문자열이 문자열의 끝과 일치하면 True가 반환되고, 주어진 문자열이 문자열의 끝과 일치하지 않으면 false가 반환됩니다. 이는 반환 유형이 System임을 나타냅니다. Boolean 및 ArgumentNullException이 발생합니다. 문자열의 끝이 일치해야 하는 입력 문자열이 null인 경우 이 방법을 사용하여 문화권 구분 비교와 함께 대소문자 구분도 확인할 수 있습니다.

구문

C# EndsWith() 메서드의 구문은 다음과 같습니다.

public bool EndsWith(String string)
public bool EndsWith(String, Boolean, CultureInfo)
public bool EndsWith (String, StringComparison)?

여기서 string은 문자열의 끝이 일치해야 하는 지정된 문자열입니다.

문자의 대소문자를 무시하려면 Boolean을 true로 설정하고, 문자의 대소문자를 고려하려면 false로 설정합니다.

CultureInfo는 지정된 문자열과 문자열의 끝을 나타내는 문자열을 비교하는 방법을 설명합니다.

StringComparison은 지정된 문자열과 문자열의 끝을 나타내는 문자열을 비교하는 방법을 설명하는 열거형 값 중 하나입니다.

C# EndsWith() 메서드 작업

  • 문자열의 끝을 나타내는 문자열이 주어진 문자열의 끝과 일치하는지 확인하기 위해 주어진 문자열과 문자열의 끝을 나타내는 문자열을 비교할 필요가 있을 때마다 EndsWith() 메서드를 사용합니다. C#의 메소드.
  • EndsWith() 메소드를 사용하여 반환된 값은 주어진 문자열과 문자열의 끝을 나타내는 문자열이 주어진 문자열의 끝과 일치하는 경우 true입니다.
  • 주어진 문자열과 문자열의 끝을 나타내는 문자열이 주어진 문자열의 끝과 일치하지 않는 경우 EndsWith() 메서드를 사용하여 반환되는 값은 false입니다.
  • 주어진 문자열의 끝과 일치하는 문자열의 끝을 나타내는 문자열과 일치할 지정된 문자열이 Null인 경우 ArgumentNullException이 발생합니다.

C# EndsWith()의 예

아래에는 다양한 예가 나와 있습니다.

예시 #1

주어진 문자열을 주어진 문자열의 끝과 문자열의 끝을 나타내는 문자열과 일치시키는 EndsWith() 메서드를 보여주는 C# 프로그램

코드:

using System;
//a class called program is defined
public class program
{
//main method is called
public static void Main(string[] args)
{
//a string variable is used to store the string whose end of the string mjst be compared with the string representing the end of the string
string str1 = "C Sharp";
//another string variable is used to store the end of the string to compare with the end of the given string
string str2 = "arp";
//another string variable is used to store the end of the string to compare with the end of the given string
string str3 = "C";
//EndsWith() method is used to compare the end of the given string and the string representing the end of a given string
Console.WriteLine("If the end of the given string matches with the string representing the end of the string:{0} ", str1.EndsWith(str2));
Console.WriteLine("If the end of the given string matches with the string representing the end of the string:{0} ",str1.EndsWith(str3));
}
}

출력:

C# 끝으로()

위 프로그램에는 program이라는 클래스가 정의되어 있습니다. 그런 다음 문자열 변수를 사용하여 문자열의 끝을 문자열의 끝을 나타내는 문자열과 비교해야 하는 문자열을 저장하는 기본 메서드가 호출됩니다. 그런 다음 다른 문자열 변수를 사용하여 문자열의 끝을 저장하여 주어진 문자열의 끝과 비교할 수 있습니다. 그런 다음 또 다른 문자열 변수를 사용하여 문자열의 끝을 저장하여 주어진 문자열의 끝과 비교할 수 있습니다. 그런 다음 EndsWith() 메서드를 사용하여 주어진 문자열의 끝과 주어진 문자열의 끝을 나타내는 문자열을 비교합니다. 이는 주어진 문자열의 끝이 문자열의 끝을 나타내는 문자열과 일치하는지 또는 일치하는지에 따라 true 또는 false를 반환합니다. 아닙니다.

예시 #2

주어진 문자열을 주어진 문자열의 끝과 문자열의 끝을 나타내는 문자열과 일치시키는 EndsWith() 메서드를 보여주는 C# 프로그램

코드:

using System;
//a class called program is defined
public class program
{
//main method is called
public static void Main(string[] args)
{
//a string variable is used to store the string whose end of the string must be compared with the string representing the end of the string
string str1 = "Learning";
//another string variable is used to store the end of the string to compare with the end of the given string
string str2 = "Learn";
//another string variable is used to store the end of the string to compare with the end of the given string
string str3 = "ing";
//EndsWith() method is used to compare the end of the given string and the string representing the end of a given string
Console.WriteLine("If the end of the given string matches with the string representing the end of the string:{0} ", str1.EndsWith(str2));
Console.WriteLine("If the end of the given string matches with the string representing the end of the string:{0} ",str1.EndsWith(str3));
}
}

출력:

C# 끝으로()

위 프로그램에는 program이라는 클래스가 정의되어 있습니다. 그런 다음 문자열 변수를 사용하여 문자열의 끝을 문자열의 끝을 나타내는 문자열과 비교해야 하는 문자열을 저장하는 기본 메서드가 호출됩니다. 그런 다음 다른 문자열 변수를 사용하여 문자열의 끝을 저장하여 주어진 문자열의 끝과 비교할 수 있습니다. 그런 다음 또 다른 문자열 변수를 사용하여 문자열의 끝을 저장하여 주어진 문자열의 끝과 비교할 수 있습니다. 그런 다음 EndsWith() 메서드를 사용하여 주어진 문자열의 끝과 주어진 문자열의 끝을 나타내는 문자열을 비교합니다. 이는 주어진 문자열의 끝이 문자열의 끝을 나타내는 문자열과 일치하는지 또는 일치하는지에 따라 true 또는 false를 반환합니다. 아니다. 마지막으로 출력은 위의 스냅샷에 표시됩니다.

위 내용은 C# 끝으로()의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:C# 인턴()다음 기사:C# 인턴()