首頁  >  文章  >  後端開發  >  C# 字串函數

C# 字串函數

王林
王林原創
2024-09-03 15:14:01623瀏覽

字串是C#程式語言最本質的部分,也是包括C#在內的現代語言中重要的資料類型之一。字串資料類型在 .NET 基底類別庫中定義,它是字元的集合,其中每個字元都是 Unicode 字元。關鍵字字串是 System 的一個物件。字串類型,用於表示稱為文字和字串的連續字元集合。  關鍵字由兩種類型組成:字串和用於宣告字串變數的字串。 string 和 String 相當相同,因此您可以使用您喜歡的命名約定來定義字串變數。為了避免 NullReferenceException,請在 null 的情況下使用空值初始化字串。

C# 中的字串函數範例

C# 程式設計中提供了預先定義的字串函數,讓我們透過範例來了解如何在 C# 程式設計中使用字串函數

1.克隆()

Clone 傳回 String 的實例。換句話說,它會傳回該資料的另一個副本。返回值將僅僅是類似資料的另一個視圖。 Clone() 方法不帶任何參數。

範例

String _string1="StringFunctions";
String _string2 = (String)_string1.Clone();
// To display both strings
Console.WriteLine("String : {0}", _string1);
Console.WriteLine("Clone String : {0}", _string2);

輸出:

字串              : 字串函數

複製字串    : StringFunctions

2.比較()

CompareTo() 方法用於將字串實例與特定的 String 物件進行比較。它檢查字串出現是否與特定字串出現在同一位置。一旦與字串進行比較,它就會傳回一個整數值作為輸出。

範例:

string _string1 = "Welcome";
string _string2 = " Welcome ";
string _string3 = "C# Coding";
Console.WriteLine(_string1.CompareTo(_string2));
Console.WriteLine(_string2.CompareTo(_string3));

輸出:

0

1

3.包含()

Contains() 方法用於傳回一個值,該值表示特定子字串是否出現在該字串中。如果在此字串中找到特定的子字串,則傳回 true,否則傳回 false。此方法的傳回值為 true 或 false 布林值。

範例:

string _string1 = " Welcome ";
string _string2 = " Welcome ";
string _string3 = "StringFunctions";
Console.WriteLine(_string1. Contains(_string2));
Console.WriteLine(_string2. Contains(_string3));

輸出:

正確

錯誤

4. EndsWith()

EndsWith() 方法用於驗證特定字串是否與該字串的結尾相符。如果特定字串出現在該字串的末尾,則結果將為 true,否則為 false。此方法的傳回值為 true 或 false 布林值。

範例:

string _string1 = " Welcome ";
string _string2 = " ome ";
string _string3 = "ing";
Console.WriteLine(_string1. EndsWith(_string2));
Console.WriteLine(_string2. EndsWith(_string3));

輸出:

正確

錯誤

5.等於()

Equals() 方法用來比較兩個特定 String 物件是否具有相同的值。如果兩個字串具有相似的值,則傳回 true,否則傳回 false。 Equals() 方法的傳回值為 true 或 false 布林值。

範例:

string _string1 = " Welcome ";
string _string2 = " Welcome ";
string _string3 = "Strings";
Console.WriteLine(_string1. Equals(_string2));
Console.WriteLine(_string2. Equals(_string3));

輸出:

正確

錯誤

6. GetHashCode()

GetHashCode() 方法用於取得指定字串的雜湊碼。它傳回一個整數值。 GetHashCode()的回傳值是字串物件的雜湊碼。

範例:

string _ string1 = "String Functions";
Console.WriteLine(_string1.GetHashCode());

輸出:

1085385658

7. GetType()

GetType()方法用於取得目前物件的類型。它返回系統。用於反射的目前實例的類型。

範例:

string _string1 = "String Functions";
Console.WriteLine(_string1.GetType ());

輸出:

系統.String

8. IndexOf()

IndexOf() 用於取得字串中存在的特定字元的索引。它將特定字元第一次出現的索引位置傳回為整數值。

範例:

string _string1 = "String Functions";
int index = _string1.IndexOf('t');
Console.WriteLine(index);

輸出:

1

9. ToLower()

這個C#字串函數用來將字串轉換為小寫。它傳回一個小寫字串。 ToLower()的回傳值是一個字串。

範例:

string _string1 = "String Functions";
string _string2 = _string1.ToLower();
Console.WriteLine(_string2 );

輸出:

字串函數

10。 ToUpper()

ToUpper() 方法用於將字串轉換為大寫。 ToUpper()的回傳值是一個字串。

範例:

string _string1 = "String Functions";
string _string2 = _string1.ToUpper();
Console.WriteLine(_string2 );

輸出:

字串函數

11。插入()

Insert() 方法用於在指定索引號處插入特定字串。索引號從0開始。插入特定字串後,它會傳回一個新的修改後的字串。 Insert()的回傳值是一個新的修改後的字串。

範例:

string _string1 = "String Functions";
string _string2 = _string1.Insert(6,"-");
Console.WriteLine(_string2 );

輸出:

字串-函數

12。長度

Length 是字串屬性,它傳回字串中的字元數,這裡空格算是字元。

範例:

string _string1 = "String Functions";
Console.WriteLine(_string1.Length);

輸出:

16

13. Replace()

This string function in C# is used to replaces the character to get another string in which all occurrences of a particular character in this string are replaced with another specified character.

Example:

string _string1 = "Strings in F#";
string _string2 = _string1.Replace('F','C');
Console.WriteLine(_string2 );

Output:

Strings in C#

14. Split()

Split() method is used to split the string based on the specified value of characters in an array. The return value of this method is the string array.

Example:

string _string1 = "Welcome C Sharp";
string[] _string2 = _string1.Split(' ');
foreach (string _string3 in _string2)
{
Console.WriteLine(_string3);
}

Output:

Welcome
C
Sharp

15. Substring()

SubString() method is used to retrieve a substring from the current occurrence of the String. The parameter “startIndex” will denote the initial position of substring and then substring will continue to the end of the string. The return value type is System. String.

Example:

string _string1 = " Hello C Sharp";
string _string2 = _string1.Substring(5);
string _string3 = " StringFunction";
string _string4 = _string3.Substring(0,8);
string _string5 = " StringFunction";
string _string6 = _string5.Substring(6,4);
Console.WriteLine(_string2);
Console.WriteLine(_string4);
Console.WriteLine(_string6);

Output:

C Sharp

StringFu

Func

Conclusion

 In this article, we learned the basics of strings in C# and how to use the String functions available in C#. Hope this article would have helped out you in understanding String Methods using C#

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 函數下一篇:C# 函數