C# - 文字列
C# では文字列を文字の配列として使用できますが、より一般的な方法は string を使用することです 文字列変数を宣言するキーワード。 string キーワードは System.String のエイリアスです クラス。
文字列オブジェクトの作成
次のいずれかの方法を使用して文字列オブジェクトを作成できます −
-
文字列リテラルを文字列変数に代入する
-
String クラスのコンストラクターを使用する
-
文字列連結演算子 (+) を使用する
-
プロパティを取得するか、文字列を返すメソッドを呼び出す
-
書式設定メソッドを呼び出して、値またはオブジェクトをその文字列表現に変換する
次の例はこれを示しています-
using System;
namespace StringApplication {
class Program {
static void Main(string[] args) {
//from string literal and string concatenation
string fname, lname;
fname = "Rowan";
lname = "Atkinson";
char []letters= { 'H', 'e', 'l', 'l','o' };
string [] sarray={ "Hello", "From", "Tutorials", "Point" };
string fullname = fname + lname;
Console.WriteLine("Full Name: {0}", fullname);
//by using string constructor { 'H', 'e', 'l', 'l','o' };
string greetings = new string(letters);
Console.WriteLine("Greetings: {0}", greetings);
//methods returning string { "Hello", "From", "Tutorials", "Point" };
string message = String.Join(" ", sarray);
Console.WriteLine("Message: {0}", message);
//formatting method to convert a value
DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
string chat = String.Format("Message sent at {0:t} on {0:D}", waiting);
Console.WriteLine("Message: {0}", chat);
}
}
}
上記のコードをコンパイルして実行すると、次の結果が生成されます −
Full Name: RowanAtkinson Greetings: Hello Message: Hello From Tutorials Point Message: Message sent at 5:58 PM on Wednesday, October 10, 2012
文字列クラスのプロパティ
String クラスには次の 2 つのプロパティがあります −
| Sr.No. | プロパティと説明 |
|---|---|
| 1 | 文字 Char を取得します 現在の String の指定された位置にあるオブジェクト オブジェクト。 |
| 2 | 長さ 現在の String オブジェクトの文字数を取得します。 |
文字列クラスのメソッド
String クラスには、文字列オブジェクトの操作に役立つ多数のメソッドがあります。次の表は、最も一般的に使用されるメソッドの一部を示しています −
| Sr.No. | 方法と説明 |
|---|---|
| 1 | public static int Compare(string strA, string strB) 指定された 2 つの文字列オブジェクトを比較し、並べ替え順序での相対的な位置を示す整数を返します。 |
| 2 | public static int Compare(string strA, string strB, bool ignoreCase ) 指定された 2 つの文字列オブジェクトを比較し、並べ替え順序での相対位置を示す整数を返します。ただし、ブール値パラメーターが true の場合、大文字と小文字は区別されません。 |
| 3 | public static string Concat(string str0, string str1) 2 つの文字列オブジェクトを連結します。 |
| 4 | public static string Concat(string str0, string str1, string str2) 3 つの文字列オブジェクトを連結します。 |
| 5 | public static string Concat(string str0, string str1, string str2, string str3) 4 つの文字列オブジェクトを連結します。 |
| 6 | public bool Contains(文字列値) 指定された String オブジェクトがこの文字列内にあるかどうかを示す値を返します。 |
| 7 | public static string Copy(string str) 指定された文字列と同じ値を持つ新しい String オブジェクトを作成します。 |
| 8 | public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) 指定された数の文字を String オブジェクトの指定された位置から Unicode 文字の配列内の指定された位置にコピーします。 |
| 9 | public bool EndsWith(文字列値) 文字列オブジェクトの末尾が指定された文字列と一致するかどうかを判断します。 |
| 10 | public bool Equals(文字列値) 現在の String オブジェクトと指定された String オブジェクトが同じ値を持つかどうかを判断します。 |
| 11 | public static bool Equals(string a, string b) 指定された 2 つの String オブジェクトが同じ値を持つかどうかを判断します。 |
| 12 | public static string Format(string format, Object arg0) 指定された文字列内の 1 つ以上の書式項目を、指定されたオブジェクトの文字列表現に置き換えます。 |
| 13 | public int IndexOf(char value) 現在の文字列で指定された Unicode 文字が最初に出現した位置のゼロから始まるインデックスを返します。 |
| 14 | public int IndexOf(文字列値) このインスタンスで指定された文字列が最初に出現した位置のゼロから始まるインデックスを返します。 |
| 15 | public int IndexOf(char value, int startIndex) この文字列内で指定された Unicode 文字が最初に出現した位置のゼロから始まるインデックスを返し、指定された文字位置から検索を開始します。 |
| 16 | public int IndexOf(string value, int startIndex) このインスタンスで指定された文字列が最初に出現した位置の 0 から始まるインデックスを返し、指定された文字位置から検索を開始します。 |
| 17 | public int IndexOfAny(char[] anyOf) 指定された Unicode 文字の配列内の任意の文字が、このインスタンスで最初に出現する位置のゼロから始まるインデックスを返します。 |
| 18 | public int IndexOfAny(char[] anyOf, int startIndex) 指定された文字位置から検索を開始し、指定された Unicode 文字配列内の任意の文字がこのインスタンスで最初に出現した位置のゼロから始まるインデックスを返します。 |
| 19 | public string Insert(int startIndex, string value) 指定された文字列が現在の文字列オブジェクトの指定されたインデックス位置に挿入された新しい文字列を返します。 |
| 20 | public static bool IsNullOrEmpty(文字列値) 指定された文字列が null か空の文字列かを示します。 |
| 21 | public static string Join(文字列セパレータ、params string[] 値) 各要素間に指定されたセパレーターを使用して、文字列配列のすべての要素を連結します。 |
| 22 | public static string Join(文字列セパレータ、string[] 値、int startIndex、int count) 各要素間に指定されたセパレーターを使用して、文字列配列の指定された要素を連結します。 |
| 23 | public int LastIndexOf(char value) 現在の文字列オブジェクト内で指定された Unicode 文字が最後に出現した位置のゼロから始まるインデックス位置を返します。 |
| 24 | public int LastIndexOf(文字列値) 現在の文字列オブジェクト内で指定された文字列が最後に出現した位置のゼロから始まるインデックス位置を返します。 |
| 25 | public string Remove(int startIndex) 指定された位置から最後の位置まで、現在のインスタンスのすべての文字を削除し、文字列を返します。 |
| 26 | public string Remove(int startIndex, int count) 指定された位置から始まる現在の文字列の指定された数の文字を削除し、文字列を返します。 |
| 27 | public string Replace(char oldChar, char newChar) 現在の文字列オブジェクト内の指定された Unicode 文字をすべて指定された Unicode 文字に置き換え、新しい文字列を返します。 |
| 28 | public string Replace(string oldValue, string newValue) 現在の文字列オブジェクト内の指定された文字列をすべて指定された文字列に置き換え、新しい文字列を返します。 |
| 29 | public string[] Split(params char[]区切り文字) 指定された Unicode 文字配列の要素で区切られた、現在の文字列オブジェクトの部分文字列を含む文字列配列を返します。 |
| 30 | public string[] Split(char[]区切り文字、整数カウント) 指定された Unicode 文字配列の要素で区切られた、現在の文字列オブジェクトの部分文字列を含む文字列配列を返します。 int パラメータは、返される部分文字列の最大数を指定します。 |
| 31 | public bool StartsWith(文字列値) この文字列インスタンスの先頭が指定された文字列と一致するかどうかを判断します。 |
| 32 | public char[] ToCharArray() 現在の文字列オブジェクトのすべての文字を含む Unicode 文字配列を返します。 |
| 33 | public char[] ToCharArray(int startIndex, int length) 指定されたインデックスから指定された長さまで、現在の文字列オブジェクト内のすべての文字を含む Unicode 文字配列を返します。 |
| 34 | 公開文字列 ToLower() この文字列のコピーを小文字に変換して返します。 |
| 35 | 公開文字列 ToUpper() この文字列のコピーを大文字に変換して返します。 |
| 36 | 公開文字列 Trim() 現在の String オブジェクトから先頭と末尾の空白文字をすべて削除します。 |
メソッドと String クラス コンストラクターの完全なリストについては、MSDN ライブラリにアクセスしてください。
例
次の例は、上記の方法のいくつかを示しています −
文字列の比較
using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string str1 = "This is test";
string str2 = "This is text";
if (String.Compare(str1, str2) == 0) {
Console.WriteLine(str1 + " and " + str2 + " are equal.");
} else {
Console.WriteLine(str1 + " and " + str2 + " are not equal.");
}
Console.ReadKey() ;
}
}
}
上記のコードをコンパイルして実行すると、次の結果が生成されます −
This is test and This is text are not equal.
文字列には文字列が含まれています
using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string str = "This is test";
if (str.Contains("test")) {
Console.WriteLine("The sequence 'test' was found.");
}
Console.ReadKey() ;
}
}
}
上記のコードをコンパイルして実行すると、次の結果が生成されます −
The sequence 'test' was found.
部分文字列の取得
using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string str = "Last night I dreamt of San Pedro";
Console.WriteLine(str);
string substr = str.Substring(23);
Console.WriteLine(substr);
}
}
}
上記のコードをコンパイルして実行すると、次の結果が生成されます −
San Pedro
弦の結合
using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string[] starray = new string[]{"Down the way nights are dark",
"And the sun shines daily on the mountain top",
"I took a trip on a sailing ship",
"And when I reached Jamaica",
"I made a stop"};
string str = String.Join("\n", starray);
Console.WriteLine(str);
}
}
}
上記のコードをコンパイルして実行すると、次の結果が生成されます −
Down the way nights are dark And the sun shines daily on the mountain top I took a trip on a sailing ship And when I reached Jamaica I made a stop
C言語