C# 字符串(String)
C# では、文字配列を使用して文字列を表すこともできますが、より一般的な方法は、stringキーワードを使用して文字列変数を宣言することです。string キーワードは、System.Stringクラスの別名。
String オブジェクトの作成
string オブジェクトを作成するには、次のいずれかの方法を使用できます。
- String 変数に文字列を代入する。
- String クラスのコンストラクターを使用する。
- 文字列連結演算子( + )を使用する。
- プロパティを取得するか、文字列を返すメソッドを呼び出す。
- 書式指定メソッドを使用して、値またはオブジェクトをその文字列表現に変換する。
次の例はこれを示しています:
实例
using System;
namespace StringApplication
{
class Program
{
static void Main(string[] args)
{
//字符串,字符串连接
string fname, lname;
fname = "Rowan";
lname = "Atkinson";
string fullname = fname + lname;
Console.WriteLine("Full Name: {0}", fullname);
//通过使用 string 构造函数
char[] letters = { 'H', 'e', 'l', 'l','o' };
string greetings = new string(letters);
Console.WriteLine("Greetings: {0}", greetings);
//方法返回字符串
string[] sarray = { "Hello", "From", "Tutorials", "Point" };
string message = String.Join(" ", sarray);
Console.WriteLine("Message: {0}", message);
//値を変換するためのフォーマットメソッド
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);
Console.ReadKey() ;
}
}
}
namespace StringApplication
{
class Program
{
static void Main(string[] args)
{
//字符串,字符串连接
string fname, lname;
fname = "Rowan";
lname = "Atkinson";
string fullname = fname + lname;
Console.WriteLine("Full Name: {0}", fullname);
//通过使用 string 构造函数
char[] letters = { 'H', 'e', 'l', 'l','o' };
string greetings = new string(letters);
Console.WriteLine("Greetings: {0}", greetings);
//方法返回字符串
string[] sarray = { "Hello", "From", "Tutorials", "Point" };
string message = String.Join(" ", sarray);
Console.WriteLine("Message: {0}", message);
//値を変換するためのフォーマットメソッド
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);
Console.ReadKey() ;
}
}
}
上記のコードをコンパイルして実行すると、次の結果が生成されます。
Full Name: RowanAtkinson Greetings: Hello Message: Hello From Tutorials Point Message: Message sent at 17:58 on Wednesday, 10 October 2012
String クラスのプロパティ
String クラスには、次の 2 つのプロパティがあります。
| 序号 | プロパティ名 & 説明 |
|---|---|
| 1 | Chars 現在においてStringオブジェクトから取得Charオブジェクトの指定位置。 |
| 2 | Length 現在のStringオブジェクト内の文字数を取得します。 |
String クラスのメソッド
String クラスには、string オブジェクトを操作するためのメソッドが多数あります。次の表に、最も一般的に使用されるメソッドの一部を示します。
| 序号 | メソッド名 & 説明 |
|---|---|
| 1 | public static int Compare(
string strA,
string strB
)
指定された 2 つの string オブジェクトを比較し、並べ替え順序における相対位置を示す整数を返します。このメソッドは大文字と小文字を区別します。 |
| 2 | public static int Compare(
string strA,
string strB,
bool ignoreCase
)
指定された 2 つの string オブジェクトを比較し、並べ替え順序における相対位置を示す整数を返します。ただし、ブール型パラメーターが true の場合、このメソッドは大文字と小文字を区別しません。 |
| 3 | public static string Concat(
string str0,
string str1
)
2つの string オブジェクトを連結します。 |
| 4 | public static string Concat(
string str0,
string str1,
string str2
)
3つの string オブジェクトを連結します。 |
| 5 | public static string Concat(
string str0,
string str1,
string str2,
string str3
)
4つの string オブジェクトを連結します。 |
| 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
)
string オブジェクトの末尾が指定された文字列と一致するかどうかを判断します。 |
| 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 文字が現在の文字列内で最初に出現する位置のインデックスを返します。インデックスは 0 から始まります。 |
| 14 | public int IndexOf(
string value
)
指定した文字列がこのインスタンス内で最初に出現する位置のインデックスを返します。インデックスは 0 から始まります。 |
| 15 | public int IndexOf(
char value,
int startIndex
)
指定した Unicode 文字が、この文字列内の指定した文字位置から検索を開始して最初に出現するインデックスを返します。インデックスは 0 から始まります。 |
| 16 | public int IndexOf(
string value,
int startIndex
)
指定した文字列が、このインスタンス内の指定した文字位置から検索を開始して最初に出現するインデックスを返します。インデックスは 0 から始まります。 |
| 17 | public int IndexOfAny(
char[] anyOf
)
指定した Unicode 文字配列内の任意の文字が、このインスタンス内で最初に出現するインデックスを返します。インデックスは 0 から始まります。 |
| 18 | public int IndexOfAny(
char[] anyOf,
int startIndex
)
指定した Unicode 文字配列内の任意の文字が、このインスタンス内の指定した文字位置から検索を開始して最初に出現するインデックスを返します。インデックスは 0 から始まります。 |
| 19 | public string Insert(
int startIndex,
string value
)
指定された文字列が現在の string オブジェクトの指定されたインデックス位置に挿入された新しい文字列を返します。 |
| 20 | public static bool IsNullOrEmpty(
string value
)
指定された文字列が null であるか、空の文字列であるかを示します。 |
| 21 | public static string Join(
string separator,
string[] value
)
文字列配列のすべての要素を連結し、各要素を指定された区切り記号で区切ります。 |
| 22 | public static string Join(
string separator,
string[] value,
int startIndex,
int count
)
文字列配列内の指定された位置から始まる指定された要素を連結し、各要素を指定された区切り記号で区切ります。 |
| 23 | public int LastIndexOf(
char value
)
指定された Unicode 文字が現在の string オブジェクト内で最後に出現する位置のインデックスを返します。インデックスは 0 から始まります。 |
| 24 | public int LastIndexOf(
string value
)
指定された文字列が現在の string オブジェクト内で最後に出現する位置のインデックスを返します。インデックスは 0 から始まります。 |
| 25 | public string Remove(
int startIndex
)
現在のインスタンス内のすべての文字を、指定した位置から最後の位置まで削除し、文字列を返します。 |
| 26 | public string Remove(
int startIndex,
int count
)
現在の文字列の指定された位置から指定された数の文字を削除し、その文字列を返します。 |
| 27 | public string Replace(
char oldChar,
char newChar
)
現在の string オブジェクト内の指定された Unicode 文字をすべて、別の指定された Unicode 文字に置き換え、新しい文字列を返します。 |
| 28 | public string Replace(
string oldValue,
string newValue
)
現在の string オブジェクト内の指定された文字列をすべて、別の指定された文字列に置き換え、新しい文字列を返します。 |
| 29 | public string[] Split(
params char[] separator
)
現在の string オブジェクト内の部分文字列を含む文字列配列を返します。部分文字列は、指定された Unicode 文字配列内の要素を使用して区切られます。 |
| 30 | public string[] Split(
char[] separator,
int count
)
現在の string オブジェクト内の部分文字列を含む文字列配列を返します。部分文字列は、指定された Unicode 文字配列内の要素を使用して区切られます。int パラメーターは、返す部分文字列の最大数を指定します。 |
| 31 | public bool StartsWith(
string value
)
文字列インスタンスの先頭が、指定した文字列と一致するかどうかを判断します。 |
| 32 | public char[] ToCharArray() 現在の string オブジェクト内のすべての文字を含む Unicode 文字配列を返します。 |
| 33 | public char[] ToCharArray(
int startIndex,
int length
)
現在の string オブジェクト内のすべての文字を含む Unicode 文字配列を、指定されたインデックスから指定された長さまで返します。 |
| 34 | public string ToLower() 文字列を小文字に変換して返します。 |
| 35 | public string ToUpper() 文字列を大文字に変換して返します。 |
| 36 | public string Trim() 現在の String オブジェクトから、先頭および末尾の空白文字をすべて削除します。 |
上記のメソッドの一覧はすべてを網羅しているわけではありません。MSDN ライブラリにアクセスして、メソッドの完全な一覧と String クラスのコンストラクターを参照してください。
实例
次の例は、上記で説明したいくつかのメソッドを示しています。
比较字符串
实例
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() ;
}
}
}
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() ;
}
}
}
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);
Console.ReadKey() ;
}
}
}
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);
Console.ReadKey() ;
}
}
}
上記のコードをコンパイルして実行すると、次の結果が生成されます。
Last night I dreamt of San Pedro 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);
Console.ReadKey() ;
}
}
}
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);
Console.ReadKey() ;
}
}
}
上記のコードをコンパイルして実行すると、次の結果が生成されます。
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其他扩展