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