我有一个随机字符串,需要知道其末尾的非字母/非数字字符的数量。
例如:
"Some text."应该导致1
"More text 123 !.?"应该导致4(包括空格)
"Even more text 123"应为0
我怎样才能做到这一点?
我通过thrift协议从c ++后端获取一些信息,包含带有德语变音符号的字符串(名称).现在这些变音符号显示为问号,所以我认为我正在尝试将它们转换为utf-8,尽管thrift似乎传递字符串为utf-8无论如何.
原始数据来自postgresql数据库,并在将其发送到thrift接口之前正确显示在c ++代码中.
我已经尝试了3种不同的版本进行转换,但它们都没有真正做任何事情我被困在这里.
版本1:
private string ConvertUTF8(string str) // str == "Ha?loch, ?mely"
{
byte[] bytSrc;
byte[] bytDestination;
string strTo = string.Empty;
bytSrc = Encoding.Unicode.GetBytes(str);
bytDestination = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, bytSrc);
strTo = Encoding.UTF8.GetString(bytDestination);
return strTo; // strTo == "Ha?loch, ?mely"
}
Run Code Online (Sandbox Code Playgroud)
版本2:
private string ConvertUTF8(string str) // str == "Ha?loch, ?mely"
{
byte[] bytes = str.Select(c => (byte)c).ToArray();
return Encoding.UTF8.GetString(bytes); // == "Ha?loch, ?mely"
}
Run Code Online (Sandbox Code Playgroud)
版本3:
private string ConvertUTF8(string str) // str == "Ha?loch, ?mely"
{
byte[] bytes …Run Code Online (Sandbox Code Playgroud)