1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

csharp / linq > 大文字小文字関係なしで同じ文字をSelect > .Where(o => string.Compare(o, term, StringComparison.CurrentCultureIgnoreCase) == 0);

Last updated at Posted at 2015-11-01

hello / Hello / heLLo をまとめてSelectしたい。

try1 (ToLowerInvariant()使用)

参考
https://msdn.microsoft.com/ja-jp/library/bb546166.aspx

ToLowerInvariant()で小文字に変換したもの同士で == 演算すればいい。

using System;
using System.Linq;

public class Test
{
	public static void Main()
	{
		string [] test = new string[3];
		test[0] = "hello";
		test[1] = "Hello";
		test[2] = "heLLo";
		
		string term = "hellO";
		
		var strs = test
			.Where(o => o.ToLowerInvariant() == term.ToLowerInvariant());

		foreach(var x in strs) {
			Console.WriteLine(x);
		}
	}
}
結果
Success	time: 0.05 memory: 24008 signal:0
hello
Hello
heLLo

try2 (string.Compare()使用; こちらの方が良い)

@Temarin_PITA さんに教えていただいた方法。
コードはTemarin_PITAさんのコメントを参照。

LINQを使う場合、インスタンスメソッドやnullなどの考慮が必要でした。今後はそういう部分も意識していければと思います。

1
2
4

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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?