Skip to content

Instantly share code, notes, and snippets.

@chuongmep

chuongmep/*Number*

Last active Aug 26, 2020
Embed
What would you like to do?
Folder funtion Number
/// <summary>
/// Return Number Join In String Example : (hello 23 my 28) return [2328]
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static IEnumerable<string> NumberInString(string str)
{
string b = string.Empty;
string[] spearator = {"."};
List<double> result = new List<double>();
for (int i=0; i< str.Length; i++)
{
if (char.IsDigit(str[i]))
b += str[i];
}
if (b.Length > 0)
{
if (int.TryParse(b,out int int32))
{
result.Add(int32);
}
else if (double.TryParse(b,out double val))
{
result.Add(val);
}
}
return result.Select(x => x.ToString(CultureInfo.InvariantCulture));
}
/// <summary>
/// Return Number split In String : Example : (hello 23 my 28) return [23,28]
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static IEnumerable<string> NumberInStrings (string str)
{
var nums = new List<double>();
var start = -1;
for (int i = 0; i < str.Length; i++)
{
if (start < 0 && char.IsDigit(str[i]))
{
start = i;
}
else if (start >= 0 && !char.IsDigit(str[i]))
{
nums.Add(double.Parse(str.Substring(start, i - start)));
start = -1;
}
}
if (start >= 0)
nums.Add(double.Parse(str.Substring(start, str.Length - start)));
return nums.Select(x=>x.ToString(CultureInfo.InvariantCulture));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.