【C#】文字列比較あれこれ
2020年5月17日
					よく使う System.String の文字列比較メモ C#限定
先頭の文字列が等しいかどうか
public bool StartsWith (string value);// 例
// 何かしらの文字列の配列
void hoge( string[] flies)
{
	// 先頭の文字列が "Assets/hoge/" のみ列挙
	foreach( var file in flies.Where( x => x.StartsWith( @"Assets/hoge/")))
	{
		// 何かしらの処理
		Log( $"file={file}"); ログに出力など
	}
}末尾の文字列が等しいかどうか
public bool EndsWith (string value);// 例
// 何かしらの文字列の配列
void hoge( string[] flies)
{
	// 末尾の文字列が ".xml" のみ列挙
	foreach( var file in flies.Where( x => x.EndsWith( @".xml")))
	{
		// 何かしらの処理
		ToJson( file); // Jsonに変換など
	}
}等しいかどうか
public bool Equals (string value);// 例
// 何かしらの文字列
void hoge( string item)
{
	if( item.Equals( "お宝"))
	{
		// item が お宝だったら ゲットだぜ
		Log( "ゲットだぜ");
	}
}含まれているかどうか
public bool Contains (string value);// 例
// 何かしらの文字列
void hoge( string nodeName)
{
	// 文字列に _Temp_ が含まれているかどうか
	if( nodeName.Contains( "_Temp_"))
	{
		// テンポラリらしい
	}
}Nullまたは空の文字列かどうか
public static bool System.String.IsNullOrEmpty (string value);// 例
// 何かしらの文字列
void hoge( string cellText)
{
	if( string.IsNullOrEmpty( cellText))
	{
		// 中断
		return;
	}
	// 処理
	// ...
}Nullまたは 空または空白だけの文字列
public static bool System.String.IsNullOrWhiteSpace (string value);// 例
// 何かしらの文字列
void hoge( string fileName)
{
	if( string.IsNullOrWhiteSpace( fileName))
	{
		// 中断
		return;
	}
	// 処理
	// ...
}
サンプル
アセットにファイルがインポートされたときに XML を Json に変換などする
//----------------------------------------------------------------------------
// アセットがインポートされたあとに呼び出されるよ
public static void OnPostprocessAllAssets(
	string[] importedAssets,
	string[] deletedAssets,
	string[] movedAssets,
	string[] movedFromAssetPaths
)
{
	// "Assets/#Resources/" というディレクトリ内で
	// .xml という拡張子で
	// ".~lock." という文字列を含んでなければ
	var xmlfiles = importedAssets
		.Where( x => x.StartsWith( "Assets/#Resources/") )
		.Where( x => x.EndsWith( @".xml") )
		.Where( x => !x.Contains( ".~lock."));
	// xml を json に変換とか
	foreach( var xmlfile in xmlfiles)
	{
		ToJson( xmlfile);
	}
}
余談
一般的な Unicode の世界をチラ見したらカオスだったのでそっ閉じした。
こちらのサイト様が面白いです。
C++標準化委員会、ついに文字とは何かを理解する: char8_t