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

更新於: 2019 年 12 月 3 日

4K+ 次瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.