Skip to content

Instantly share code, notes, and snippets.

@chuongmep
Last active August 26, 2020 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuongmep/79712c483c121be4313af95be9119640 to your computer and use it in GitHub Desktop.
Save chuongmep/79712c483c121be4313af95be9119640 to your computer and use it in GitHub Desktop.
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