C#의 StartsWith() 메서드는 이 문자열 인스턴스의 시작 부분이 지정된 문자열과 일치하는지 여부를 확인하는 데 사용됩니다.
구문
public bool StartsWith (string val);
위의 val은 비교할 문자열입니다.
예
using System;
public class Demo {
public static void Main() {
string str = "JohnAndJacob";
Console.WriteLine("String = "+str);
Console.WriteLine("Does String begins with J? = "+str.StartsWith("J"));
char[] destArr = new char[20];
str.CopyTo(1, destArr, 0, 4);
Console.Write(destArr);
}
} 출력
그러면 다음과 같은 출력이 생성됩니다. -
String = JohnAndJacob Does String begins with J? = True ohnA
예
이제 다른 예를 살펴보겠습니다. -
using System;
public class Demo {
public static void Main(String[] args) {
string str1 = "Akon";
string str2 = "Eminem";
Console.WriteLine("String 1 = "+str1);
Console.WriteLine("HashCode of String 1 = "+str1.GetHashCode());
Console.WriteLine("Does String1 begins with E? = "+str1.StartsWith("E"));
Console.WriteLine("\nString 2 = "+str2);
Console.WriteLine("HashCode of String 2 = "+str2.GetHashCode());
Console.WriteLine("Does String2 begins with E? = "+str2.StartsWith("E"));
Console.WriteLine("\nString 1 is equal to String 2? = {0}", str1.Equals(str2));
}
} 출력
그러면 다음과 같은 출력이 생성됩니다. -
String 1 = Akon HashCode of String 1 = 416613838 Does String1 begins with E? = False String 2 = Eminem HashCode of String 2 = 40371907 Does String2 begins with E? = True String 1 is equal to String 2? = False