Substring()
C#中的方法用于从此实例检索子字符串。子字符串从指定的字符位置开始,一直到字符串的末尾。
语法如下-
public string Substring (int begnIndex); public string Substring (int begnIndex, int len);
上面的示例中,begnIndex是子字符串的从零开始的字符位置。len参数是要检索的子字符串的编号
现在让我们看一个例子-
using System; public class Demo { public static void Main(String[] args) { string str1 = "Katherine"; string str2 = "PQRS"; Console.WriteLine("String1 = "+str1); Console.WriteLine("String1 ToUpperInvariant = "+str1.ToUpperInvariant()); Console.WriteLine("String1 Substring from index4 = " + str1.Substring(4)); Console.WriteLine("\nString2 = "+str2); Console.WriteLine("String2 ToUpperInvariant = "+str2.ToLowerInvariant()); Console.WriteLine("String2 Substring from index2 = " + str2.Substring(2)); } }
输出结果
String1 = Katherine String1 ToUpperInvariant = KATHERINE String1 Substring from index4 = erine String2 = PQRS String2 ToUpperInvariant = pqrs String2 Substring from index2 = RS
现在让我们来看另一个例子-
using System; public class Demo { public static void Main(String[] args) { string str1 = "Notebook"; string str2 = "Ultrabook"; Console.WriteLine("String1 = "+str1); Console.WriteLine("String1 ToUpperInvariant = "+str1.ToUpperInvariant()); Console.WriteLine("String1 Substring from index4 = " + str1.Substring(4, 4)); Console.WriteLine("\nString2 = "+str2); Console.WriteLine("String2 ToUpperInvariant = "+str2.ToLowerInvariant()); Console.WriteLine("String2 Substring from index2 = " + str2.Substring(0, 5)); } }
输出结果
String1 = Notebook String1 ToUpperInvariant = NOTEBOOK String1 Substring from index4 = book String2 = Ultrabook String2 ToUpperInvariant = ultrabook String2 Substring from index2 = Ultra