LoginSignup
25
27

More than 5 years have passed since last update.

C#文字列操作 チートシート

Last updated at Posted at 2018-04-07

C#文字列操作 チートシート

文字列に関する操作をしたい時に痒いところに手が届く一覧。
思いついたらその都度追記していきます。

文字数・バイト数を求める

文字数を求める
int hoge = "string".Length

バイト数を求める

Encoding enc = Encoding.GetEncoding("Shift_JIS");
int hoge = enc.GetByteCount("string");

サンプルコード

int tempInt;
string halfSize = "0123456789";            // 半角10文字(10byte)
string fullSize = "0123456789";    // 全角10文字(20byte)

// 文字数を求める
tempInt = halfSize.Length; // tempIntは10
tempInt = fullSize.Length; // tempIntは10

// sjisでのbyte数を求める
Encoding enc = Encoding.GetEncoding("Shift_JIS");
tempInt = enc.GetByteCount(halfSize); // tempIntは10
tempInt = enc.GetByteCount(fullSize); // tempIntは20

文字列の前後の空白(全角・半角ともに)を削除する

前後の空白を削除
string hoge = hello.Trim();

前の空白を削除
string hoge = hello.TrimStart();

後の空白を削除
string hoge = hello.TrimEnd();

サンプルコード

string temp = "";
string hello = "          Hello,   World!!     ";

temp = hello.Trim();        // "Hello,   World!!"
temp = hello.TrimStart();   // "Hello,   World!!     "
temp = hello.TrimEnd();     // "          Hello,   World!!"

stringが空かどうかチェックする

stringがnullまたは空かどうかチェックする
bool hoge = string.IsNullOrEmpty("string")

stringがnullまたは空またはスペース(全半角問わない)かどうかチェックする
bool hoge = string.IsNullOrWhiteSpace("string")

サンプルコード

bool isTemp;

string strNull = null;

string strEmpty1 = string.Empty;
string strEmpty2 = "";

string halfSpaceOnly = "       ";
string fullSpaceOnly = "     ";

string hoge = "hogehoge";

// IsNullOrEmpty
isTemp = string.IsNullOrEmpty(strNull);         // true

isTemp = string.IsNullOrEmpty(strEmpty1);       // true
isTemp = string.IsNullOrEmpty(strEmpty2);       // true

isTemp = string.IsNullOrEmpty(halfSpaceOnly);   // false
isTemp = string.IsNullOrEmpty(fullSpaceOnly);   // false

isTemp = string.IsNullOrEmpty(hoge);            // false

// IsNullOrWhiteSpace
isTemp = string.IsNullOrWhiteSpace(strNull);        // true

isTemp = string.IsNullOrWhiteSpace(strEmpty1);      // true
isTemp = string.IsNullOrWhiteSpace(strEmpty2);      // true

isTemp = string.IsNullOrWhiteSpace(halfSpaceOnly);  // true
isTemp = string.IsNullOrWhiteSpace(fullSpaceOnly);  // true

isTemp = string.IsNullOrWhiteSpace(hoge);           // false

string同士を比較する

string同士を比較する(大文字と小文字を区別する)
bool temp = string.Equals("ABC", "Abc");

string同士を比較する(大文字と小文字を区別しない)
bool temp = string.Equals("ABC", "Abc", StringComparison.CurrentCultureIgnoreCase);

サンプルコード

bool temp;
string hoge1 = "ABC";
string hoge2 = "Abc";

temp = string.Equals(hoge1, hoge2); // 大文字と小文字を区別するためfalse
temp = string.Equals(hoge1, hoge2, StringComparison.CurrentCultureIgnoreCase); // 大文字と小文字を区別しないためtrue

大文字と小文字の変換(全半角ではないので注意)

全ての文字を大文字化
string hoge = "string".ToUpper();
全ての文字を小文字化
string hoge = "string".ToLower();
先頭のみ大文字、残りを小文字化
string temp = char.ToUpper("string"[0]) + "string".Substring(1).ToLower();

サンプルコード

string temp = "";
string name = "eRAseRmOToRpHAntOM";

temp = name.ToUpper();                                          // "ERASERMOTORPHANTOM"
temp = name.ToLower();                                          // "erasermotorphantom"
temp = char.ToUpper(name[0]) + name.Substring(1).ToLower();     // "Erasermotorphantom"

文字列の切り出し

先頭からn文字抜き出す
string temp = hoge.Substring(0, n);
m+1文字目からn文字抜き出す
string temp = hoge.Substring(m, n);
m+1文字目から最後まで抜き出す
string temp = hoge.Substring(m);

サンプルコード

string temp = "";
string hoge = "abcdefghijklmnopqrstuvwxyz";

// 先頭から5文字抜き出す abcde
temp = hoge.Substring(0, 5);

// 5文字スキップし、6文字目から2文字取得 fg
temp = hoge.Substring(5, 2);

// 22文字スキップし、それ以降の文字列を取得する wxyz
temp = hoge.Substring(22);

文字列を指定して置換または削除する

文字列を指定して置換する
string temp = "hogepiyo".Replace("hoge", "piyo");
文字列を指定して削除する
string temp = "hogepiyo".Replace("piyo", "");
サンプルコード

string temp = "";
string hoge = "hogepiyo";

// 文字列中の"hoge"を"piyo"に置換 piyopiyoになる
temp = hoge.Replace("hoge", "piyo");

// 文字列中から"piyo"を削除 hogeになる
temp = hoge.Replace("piyo", "");

文字列配列とコンマ区切りの変換

文字列配列からコンマ区切りの一行を作成する
string line = string.Join(",", strArray);

コンマ区切りの一行から文字列配列を作成する
string[] splitStr = line.Split(',');

サンプルコード

string[] elements = new string[]
{
    "apple","banana","chocolate","donut"
};

// Joinで一発。for文で最後の要素以外にコンマを追加とかしなくていい
string line = string.Join(",", elements);

// 一行をコンマで区切って配列に入れる。
string[] splitStr = line.Split(',');
25
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
27