csharp System.String

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp System.String相关的知识,希望对你有一定的参考价值。

public int StringToInt(string str)
{
    int result = 0;
    if (Regex.IsMatch(str, @"^-?[0-9]\d*"))
    {
        bool isNegative = false;
        if (str.IndexOf('-') != -1)
        {
            str = str.Substring(1);
            isNegative = true;
        }
        for (int i = 0; i < str.Length; i++)
        {
            result = result * 10 + (str[i] - '0');
        }
        result = isNegative ? result * -1 : result;
    }
    return result;
}
//Here is an extension method that illustrates one way to do it. The idea is that you can loop through each character of the string, and use char.ConvertToUtf32(string, index) to get the unicode value. If the returned value is larger than 0xFFFF, then you know that the unicode value was composed of a set of surrogate characters, and you can adjust the index value accordingly to skip the 2nd surrogate character.

public static IEnumerable<int> GetUnicodeCodePoints(this string s)
{
    for (int i = 0; i < s.Length; i++)
    {
        int unicodeCodePoint = char.ConvertToUtf32(s, i);
        if (unicodeCodePoint > 0xffff)
        {
            i++;
        }
        yield return unicodeCodePoint;
    }
}

static void Main(string[] args)
{
    string s = "a

以上是关于csharp System.String的主要内容,如果未能解决你的问题,请参考以下文章

无法将对象类型“System.String[*]”转换为“System.String[]”

如何将 System::String^ 转换为 std::string?

C++ .NET 将 System::String 转换为 std::string

如何将 SecureString 转换为 System.String?

C#中List〈string〉和string[]数组之间的相互转换

“缺少从 System.Char 到 System.String 的映射”AutoMapper 错误