string 是一個不可變(immutable)的類型,它表示一個字符序列。
以下是一些 string 類別提供的常見屬性和方法:
常見屬性
Length:
獲取字符串的長度。
Length
string str = "Hello, World\u0021";//\u0021 是 ! 的 unicode
int length = str.Length; // length 為 13
Chars:
通過索引獲取字符串中的字符。
Chars
string str = "Hello";
char firstChar = str[0]; // firstChar 為 'H'
常見方法
Concat:
連接兩個字符串。
Concat
string str1 = "Hello";
string str2 = "World";
string result = string.Concat(str1, ", ", str2); // result 為 "Hello, World"
ToUpper、ToLower:
分別將字符串轉換為大寫和小寫。
ToUpper、ToLower
string original = "Hello";
string upperCase = original.ToUpper(); // upperCase 為 "HELLO"
string lowerCase = original.ToLower(); // lowerCase 為 "hello"
Substring:
獲取字符串的子字串。
Substring
string original = "Hello, World!";
string substring = original.Substring(7, 5); // substring 為 "World"
IndexOf 和 LastIndexOf:
查找字符或子字符串的位置。
IndexOf、LastIndexOf
string sentence = "The quick brown fox jumps over the lazy dog.";
int indexOfFox = sentence.IndexOf("fox"); // indexOfFox 為 16
int lastIndexOfDog = sentence.LastIndexOf("dog"); // lastIndexOfDog 為 44
Replace:
替換字符串中的子字符串。
Replace
string original = "Hello, World!";
string modified = original.Replace("Hello", "Hi"); // modified 為 "Hi, World!"
IsNullOrEmpty 和 IsNullOrWhiteSpace:
用於檢查字符串是否為空或只包含空白字符。
IsNullOrEmpty、IsNullOrWhiteSpace
string emptyStr = "";
bool isEmpty = string.IsNullOrEmpty(emptyStr); // isEmpty 為 true
string whiteSpaceStr = " ";
bool isWhiteSpace = string.IsNullOrWhiteSpace(whiteSpaceStr); // isWhiteSpace 為 true
Trim、TrimStart 和 TrimEnd:
用於檢查字符串是否為空或只包含空白字符。
Trim、TrimStart、TrimEnd
string str = " Hello, World! ";
string trimmed = str.Trim(); // trimmed 為 "Hello, World!"
StartsWith 和 EndsWith:
檢查字符串是否以特定的字串開頭或結尾。
StartsWith、EndsWith
string str = "Hello, World!";
bool startsWithHello = str.StartsWith("Hello"); // startsWithHello 為 true
bool endsWithWorld = str.EndsWith("World!"); // endsWithWorld 為 true
Contains:
檢查字符串是否包含特定的字串。
Contains
string sentence = "The quick brown fox jumps over the lazy dog.";
bool containsFox = sentence.Contains("fox"); // containsFox 為 true
Compare 和 CompareTo:
用於比較兩個字符串。
比對方式:
變數名使用 CompareTo
string x="abc", y="AB";
x.CompareTo(y);
string 函數使用 Compare
string.Compare("abc","AB");
若前面字串 > 後面字串傳回 1
若前面字串 == 後面字串傳回 0
若前面字串 < 後面字串傳回 -1
比對方式以 ASCII 碼大小逐字比對,若皆相等則長者勝。
Compare、CompareTo
string str1 = "apple";
string str2 = "banana";
int comparisonResult1 = string.Compare(str1, str2); // comparisonResult 為 -1 (str1 < str2)
int comparisonResult2 = str1.CompareTo(str2); // comparisonResult 為 -1 (str1 < str2)
ToCharArray:
將字符串轉換為字符數組。
ToCharArray
string str = "Hello";
char[] charArray = str.ToCharArray(); // charArray 為 { 'H', 'e', 'l', 'l', 'o' }
綜合範例:
複製程式碼
using System;
class Program
{
static void Main()
{
// 創建一個字符串
string originalString = " Hello,World! ";
// 使用屬性:Length
int length = originalString.Length;
Console.WriteLine($"原始字符串 {originalString} 的長度為 {length}");
// 使用方法:Trim,去除首尾空白字符
string trimmedString = originalString.Trim();
Console.WriteLine($"去除首尾空白後的字符串:'{trimmedString}'");
bool startsWithHello = trimmedString.StartsWith("Hello"); // startsWithHello 為 true
bool endsWithWorld = trimmedString.EndsWith("John!"); // endsWithWorld 為 false
Console.WriteLine($"Hello 在前:{startsWithHello},John! 在後:{endsWithWorld}");
// 使用方法:ToUpper,將字符串轉為大寫
string upperCaseString = originalString.ToUpper();
Console.WriteLine($"大寫形式的字符串:'{upperCaseString}'");
// 使用方法:Replace,替換字符串中的子串
string replaceString = originalString.Replace("World", "John");
Console.WriteLine($"替換 World 為 John :{replaceString}");
// 使用方法:Contains,檢查字符串是否包含特定的字串
bool containsHello = originalString.Contains("Hello");
Console.WriteLine($"字符串是否包含 'Hello': {containsHello}");
// 使用方法:Substring,獲取子串
string subString = originalString.Substring(6, 5);
Console.WriteLine($"第 6 個字符開始的 5 個字符子串:'{subString}'");
string concatString = string.Concat("--- ", "Hello" , " ---");
Console.WriteLine($"字串連接:{concatString}");
string str1 = "abc";
string str2 = "ac";
Console.WriteLine($"{str1} compare {str2} = {str1.CompareTo(str2)}");
Console.WriteLine($"abc compare ab = {string.Compare("abc","ab")}");
}
}
執行結果