原生的Win32 API确实暴露了找到的字符串的长度。你可以使用P/Invoke来直接调用FindNLSStringEx
:
static class CompareInfoExtensions
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern int FindNLSStringEx(string lpLocaleName, uint dwFindNLSStringFlags, string lpStringSource, int cchSource, string lpStringValue, int cchValue, out int pcchFound, IntPtr lpVersionInformation, IntPtr lpReserved, int sortHandle);
const uint FIND_FROMSTART = 0x00400000;
public static int IndexOfEx(this CompareInfo compareInfo, string source, string value, int startIndex, int count, CompareOptions options, out int length)
{
// Argument validation omitted for brevity
return FindNLSStringEx(compareInfo.Name, FIND_FROMSTART, source, source.Length, value, value.Length, out length, IntPtr.Zero, IntPtr.Zero, 0);
}
}
static class Program
{
static void Main()
{
var s = "<<Gewerbegebiet Waldstraße>>";
//var s = "<<Gewerbegebiet Waldstrasse>>";
int length;
int start = new CultureInfo("de-DE").CompareInfo.IndexOfEx(s, "strasse", 0, s.Length, CompareOptions.None, out length);
Console.WriteLine(s.Substring(0, start) + s.Substring(start + length));
}
}
,我没有看到一个方法来做到这一点使用纯属BCL。
来源
2015-11-05 19:32:26
hvd
如何用空字符串替换它? 's = s.Replace(“strasse”,“”);' – dotctor
@dotctor我相信OP在说'string.Replace'不考虑文化,所以“ss”不符合“ß ”。 – juharr
我正在'en-US'上运行,并且遇到了这个问题。事情就是IndexOf的行为不同。 –