Friday, December 07, 2007

C# procurar strings desprezando os acentos. String search with case and accent insensitive

using System;
using System.Collections.Generic;
using System.Globalization;

public class StringHelper
{

public static int IndexOfWithCaseAndAccentInsensitive (string source, string stringToSearch)
{
CompareInfo ci = new CultureInfo("en-US").CompareInfo;
CompareOptions co = CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace;
return ci.IndexOf(source, stringToSearch, co);
}

public static void Main()
{
RL();

WL(IndexOfWithCaseAndAccentuationIncensite("zzzzzzzzzzzÁbc", "a"));

RL();
}

#region Helper methods

private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}

private static void RL()
{
Console.ReadLine();
}

private static void Break()
{
System.Diagnostics.Debugger.Break();
}

#endregion
}

Obrigado João, pode vir a ser útil.

2 comments:

Anonymous said...

Me ajudou muito.
Thanks.

Luis Jeronimo said...

grande achado :D