C# Substring() 方法
C# 中的 Substring() 方法用于从该实例检索子字符串。该子字符串从一个指定字符位置开始,并持续到字符串结尾。
语法
语法如下 -
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("
String2 = "+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("
String2 = "+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
广告