Tengo un requisito para encontrar y extraer un número contenido dentro de una cadena.
Por ejemplo, a partir de estas cadenas:
string test = "1 test"
string test1 = " 1 test"
string test2 = "test 99"
¿Cómo puedo hacer esto?
ir a través de la cuerda y usar Char.IsDigit
string a = "str123";
string b = string.Empty;
int val;
for (int i=0; i< a.Length; i++)
{
if (Char.IsDigit(a[i]))
b += a[i];
}
if (b.Length>0)
val = int.Parse(b);
b == "System.Linq.Enumerable.."
. Correcto (e incluso más simple) seríab = String.Join("", a.Where(char.IsDigit))
new string(char[])
constructor.
\d+
es la expresión regular para un número entero. Entonces
//System.Text.RegularExpressions.Regex
resultString = Regex.Match(subjectString, @"\d+").Value;
devuelve una cadena que contiene la primera aparición de un número en subjectString
.
Int32.Parse(resultString)
entonces te dará el número.
Regex.Match(subjectString, @"-?\d+").Value
en su lugar
Así es como limpio los números de teléfono para obtener solo los dígitos:
string numericPhone = new String(phone.Where(Char.IsDigit).ToArray());
string numericPhone =new String(phone.Where(Char.IsDigit).ToArray());
usar expresiones regulares ...
Regex re = new Regex(@"\d+");
Match m = re.Match("test 66");
if (m.Success)
{
Console.WriteLine(string.Format("RegEx found " + m.Value + " at position " + m.Index.ToString()));
}
else
{
Console.WriteLine("You didn't enter a string containing a number!");
}
Lo que uso para obtener números de teléfono sin puntuación ...
var phone = "(787) 763-6511";
string.Join("", phone.ToCharArray().Where(Char.IsDigit));
// result: 7877636511
Regex.Split puede extraer números de cadenas. Obtienes todos los números que se encuentran en una cadena.
string input = "There are 4 numbers in this string: 40, 30, and 10.";
// Split on one or more non-digit characters.
string[] numbers = Regex.Split(input, @"\D+");
foreach (string value in numbers)
{
if (!string.IsNullOrEmpty(value))
{
int i = int.Parse(value);
Console.WriteLine("Number: {0}", i);
}
}
Salida:
Número: 4 Número: 40 Número: 30 Número: 10
Aquí hay una Linq
versión:
string s = "123iuow45ss";
var getNumbers = (from t in s
where char.IsDigit(t)
select t).ToArray();
Console.WriteLine(new string(getNumbers));
"123iuow45ss".AsEnumerable().Where(char.IsDigit)
?
from t .. select t
redundancia, pero de todos modos, aplausos.
Otra solución simple usando Regex Debería necesitar usar esto
using System.Text.RegularExpressions;
y el código es
string var = "Hello3453232wor705Ld";
string mystr = Regex.Replace(var, @"\d", "");
string mynumber = Regex.Replace(var, @"\D", "");
Console.WriteLine(mystr);
Console.WriteLine(mynumber);
también puedes probar esto
string.Join(null,System.Text.RegularExpressions.Regex.Split(expr, "[^\\d]"));
Simplemente use un RegEx para que coincida con la cadena, luego convierta:
Match match = Regex.Match(test , @"(\d+)");
if (match.Success) {
return int.Parse(match.Groups[1].Value);
}
Aquí hay otro Linq
enfoque que extrae el primer número de una cadena.
string input = "123 foo 456";
int result = 0;
bool success = int.TryParse(new string(input
.SkipWhile(x => !char.IsDigit(x))
.TakeWhile(x => char.IsDigit(x))
.ToArray()), out result);
Ejemplos:
string input = "123 foo 456"; // 123
string input = "foo 456"; // 456
string input = "123 foo"; // 123
Para aquellos que desean un número decimal de una cadena con Regex en DOS líneas:
decimal result = 0;
decimal.TryParse(Regex.Match(s, @"\d+").Value, out result);
Lo mismo se aplica al flotador , largo , etc.
string input = "Hello 20, I am 30 and he is 40";
var numbers = Regex.Matches(input, @"\d+").OfType<Match>().Select(m => int.Parse(m.Value)).ToArray();
Puede hacer esto usando la String
propiedad como a continuación:
return new String(input.Where(Char.IsDigit).ToArray());
que da solo el número de la cadena.
var match=Regex.Match(@"a99b",@"\d+");
if(match.Success)
{
int val;
if(int.TryParse(match.Value,out val))
{
//val is set
}
}
La pregunta no indica explícitamente que solo desea los caracteres del 0 al 9, pero no sería exagerado creer que es cierto a partir de su conjunto de ejemplos y comentarios. Así que aquí está el código que hace eso.
string digitsOnly = String.Empty;
foreach (char c in s)
{
// Do not use IsDigit as it will include more than the characters 0 through to 9
if (c >= '0' && c <= '9') digitsOnly += c;
}
Por qué no desea utilizar Char.IsDigit (): los números incluyen caracteres como fracciones, subíndices, superíndices, números romanos, numeradores de monedas, números encerrados y dígitos específicos de la secuencia de comandos.
var outputString = String.Join("", inputString.Where(Char.IsDigit));
Obtenga todos los números en la cadena. Por lo tanto, si utiliza para examaple '1 plus 2' obtendrá '12'.
Método de extensión para obtener todos los números positivos contenidos en una cadena:
public static List<long> Numbers(this string str)
{
var nums = new List<long>();
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(long.Parse(str.Substring(start, i - start)));
start = -1;
}
}
if (start >= 0)
nums.Add(long.Parse(str.Substring(start, str.Length - start)));
return nums;
}
Si también desea números negativos, simplemente modifique este código para manejar el signo menos ( -
)
Dada esta entrada:
"I was born in 1989, 27 years ago from now (2016)"
La lista de números resultante será:
[1989, 27, 2016]
si el número tiene puntos decimales, puede usar a continuación
using System;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine(Regex.Match("anything 876.8 anything", @"\d+\.*\d*").Value);
Console.WriteLine(Regex.Match("anything 876 anything", @"\d+\.*\d*").Value);
Console.WriteLine(Regex.Match("$876435", @"\d+\.*\d*").Value);
Console.WriteLine(Regex.Match("$876.435", @"\d+\.*\d*").Value);
}
}
}
resultados:
"anything 876.8 anything" ==> 876.8
"anything 876 anything" ==> 876
"$ 876435" ==> 876435
"$ 876.435" ==> 876.435
Muestra: https://dotnetfiddle.net/IrtqVt
Hizo el reverso de una de las respuestas a esta pregunta: ¿Cómo eliminar números de la cadena usando Regex.Replace?
// Pull out only the numbers from the string using LINQ
var numbersFromString = new String(input.Where(x => x >= '0' && x <= '9').ToArray());
var numericVal = Int32.Parse(numbersFromString);
string verificationCode ="dmdsnjds5344gfgk65585";
string code = "";
Regex r1 = new Regex("\\d+");
Match m1 = r1.Match(verificationCode);
while (m1.Success)
{
code += m1.Value;
m1 = m1.NextMatch();
}
Aquí se proporciona un enfoque interesante por Ahmad Mageed, utiliza expresiones regulares y StringBuilder para extraer los números enteros en el orden en que aparecen en la cadena.
Un ejemplo de uso de Regex.Split basado en la publicación de Ahmad Mageed es el siguiente:
var dateText = "MARCH-14-Tue";
string splitPattern = @"[^\d]";
string[] result = Regex.Split(dateText, splitPattern);
var finalresult = string.Join("", result.Where(e => !String.IsNullOrEmpty(e)));
int DayDateInt = 0;
int.TryParse(finalresult, out DayDateInt);
Aquí está mi algoritmo
//Fast, C Language friendly
public static int GetNumber(string Text)
{
int val = 0;
for(int i = 0; i < Text.Length; i++)
{
char c = Text[i];
if (c >= '0' && c <= '9')
{
val *= 10;
//(ASCII code reference)
val += c - 48;
}
}
return val;
}
aqui esta mi solucion
string var = "Hello345wor705Ld";
string alpha = string.Empty;
string numer = string.Empty;
foreach (char str in var)
{
if (char.IsDigit(str))
numer += str.ToString();
else
alpha += str.ToString();
}
Console.WriteLine("String is: " + alpha);
Console.WriteLine("Numeric character is: " + numer);
Console.Read();
Tendrás que usar Regex como \d+
\d
coincide con dígitos en la cadena dada.
static string GetdigitFromString(string str)
{
char[] refArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char[] inputArray = str.ToCharArray();
string ext = string.Empty;
foreach (char item in inputArray)
{
if (refArray.Contains(item))
{
ext += item.ToString();
}
}
return ext;
}
string s = "kg g L000145.50\r\n";
char theCharacter = '.';
var getNumbers = (from t in s
where char.IsDigit(t) || t.Equals(theCharacter)
select t).ToArray();
var _str = string.Empty;
foreach (var item in getNumbers)
{
_str += item.ToString();
}
double _dou = Convert.ToDouble(_str);
MessageBox.Show(_dou.ToString("#,##0.00"));
Basado en la última muestra, creé un método:
private string GetNumberFromString(string sLongString, int iLimitNumbers)
{
string sReturn = "NA";
int iNumbersCounter = 0;
int iCharCounter = 0;
string sAlphaChars = string.Empty;
string sNumbers = string.Empty;
foreach (char str in sLongString)
{
if (char.IsDigit(str))
{
sNumbers += str.ToString();
iNumbersCounter++;
if (iNumbersCounter == iLimitNumbers)
{
return sReturn = sNumbers;
}
}
else
{
sAlphaChars += str.ToString();
iCharCounter++;
// reset the counter
iNumbersCounter = 0;
}
}
return sReturn;
}
1.5
? Notación exponencial como1.5E45
?