Csharp-strings
C#-文字列
C#では、文字列を文字の配列として使用できますが、より一般的な方法は、 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
文字列クラスのプロパティ
文字列クラスには、次の2つのプロパティがあります-
Sr.No. | Property & Description |
---|---|
1 |
現在の_String_オブジェクトの指定された位置にある_Char_オブジェクトを取得します。 |
2 |
Length 現在のStringオブジェクトの文字数を取得します。 |
Stringクラスのメソッド
Stringクラスには、文字列オブジェクトの操作に役立つ多数のメソッドがあります。 次の表は、最も一般的に使用される方法のいくつかを提供します-
以下に、Stringクラスのメソッドのリストを示します。
Sr.No. | Methods & Description |
---|---|
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 value) 指定された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(string value) 文字列オブジェクトの末尾が指定された文字列と一致するかどうかを判断します。 |
10 |
public bool Equals(string value) 現在の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(string value) このインスタンスで指定された文字列が最初に出現するゼロから始まるインデックスを返します。 |
15 |
public int IndexOf(char value, int startIndex) 指定された文字位置で検索を開始し、この文字列で指定されたUnicode文字が最初に出現するゼロから始まるインデックスを返します。 |
16 |
public int IndexOf(string value, int startIndex) 指定された文字位置で検索を開始し、このインスタンスで指定された文字列が最初に出現するゼロから始まるインデックスを返します。 |
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(string value) 指定された文字列がnullか空の文字列かを示します。 |
21 |
public static string Join(string separator, params string[] value) 各要素間に指定されたセパレータを使用して、文字列配列のすべての要素を連結します。 |
22 |
public static string Join(string separator, string[] value, int startIndex, int count) 各要素間に指定された区切り文字を使用して、文字列配列の指定された要素を連結します。 |
23 |
public int LastIndexOf(char value) 現在の文字列オブジェクト内で指定されたUnicode文字が最後に出現するゼロから始まるインデックス位置を返します。 |
24 |
public int LastIndexOf(string value) 現在の文字列オブジェクト内で指定された文字列が最後に出現するゼロから始まるインデックス位置を返します。 |
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[] separator) 指定されたUnicode文字配列の要素で区切られた、現在の文字列オブジェクトの部分文字列を含む文字配列を返します。 |
30 |
public string[] Split(char[] separator, int count) 指定されたUnicode文字配列の要素で区切られた、現在の文字列オブジェクトの部分文字列を含む文字配列を返します。 intパラメーターは、返される部分文字列の最大数を指定します。 |
31 |
public bool StartsWith(string value) この文字列インスタンスの先頭が指定した文字列と一致するかどうかを判断します。 |
32 |
public char[] ToCharArray() 現在の文字列オブジェクト内のすべての文字を含むUnicode文字配列を返します。 |
33 |
public char[] ToCharArray(int startIndex, int length) 指定されたインデックスから指定された長さまでの、現在の文字列オブジェクト内のすべての文字を含むUnicode文字配列を返します。 |
34 |
public string ToLower() この文字列のコピーを小文字に変換して返します。 |
35 |
public string ToUpper() この文字列のコピーを大文字に変換して返します。 |
36 |
public string 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