এই স্ট্রিং ইন্সট্যান্সের শুরু নির্দিষ্ট স্ট্রিং এর সাথে মেলে কিনা তা নির্ধারণ করতে C#-এ StartsWith() পদ্ধতি ব্যবহার করা হয়।
সিনট্যাক্স
public bool StartsWith (string 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