C# 字串屬性

PHPz
發布: 2023-09-15 19:25:02
轉載
791 人瀏覽過

C# 字符串属性

以下是C# 中String 類別的屬性-

2
#Sr.No #屬性&說明

#1 ##」 #取得目前String物件中指定位置的Char物件。

Length 取得目前 String 物件中的字元數。

範例

讓我們看一個範例-

 現場示範

using System;
public class Demo {
   public static void Main() {
      string str1 = "h8b9";
      string str2 = "abcdef";
      Console.WriteLine("Is string1 null or empty? = "+string.IsNullOrEmpty(str1));
      Console.WriteLine("Is string2 null or empty? = "+string.IsNullOrEmpty(str2));
      bool val = str1 != str2;
      Console.WriteLine("Is str1 equal to str2? = "+val);
      for (int i = 0; i < str1.Length; i++) {
         if (Char.IsLetter(str1[i]))
            Console.WriteLine(""+str1[i]+" is a letter");
         else
            Console.WriteLine(""+str1[i]+" is a number");
      }
   }
}
登入後複製

輸出

這將產生以下輸出-

Is string1 null or empty? = False
Is string2 null or empty? = False
Is str1 equal to str2? = True
h is a letter
8 is a number
b is a letter
9 is a number
登入後複製

Example

現在讓我們看另一個範例-

 現場示範

using System;
public class Demo {
   public static void Main() {
      string str1 = "hijklm";
      string str2 = String.Empty;
      Console.WriteLine("Is string1 null or whitespace? = "+String.IsNullOrWhiteSpace(str1));
      Console.WriteLine("Is string2 null or whitespace? = "+String.IsNullOrWhiteSpace(str2));
      Console.WriteLine("String1 length = "+str1.Length);
      Console.WriteLine("String2 length = "+str1.Length);
      Console.WriteLine("String length = "+"demo".Length);
   }
}
登入後複製

輸出

這將產生以下範例-

Is string1 null or whitespace? = False
Is string2 null or whitespace? = True
String1 length = 6
String2 length = 6
String length = 4
登入後複製

以上是C# 字串屬性的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!