I need to check if a string contains only numbers(integers and decimal) with a space in between them. Eg: 1 2 4.5 72 (this is acceptable); 1 7..5 3.2.1 (this is unacceptable)
Asked
Active
Viewed 280 times
-2
-
1Remove spaces from the string and try to parse the number to double. There is nothing in built available in C# – Chetan Nov 07 '19 at 04:56
-
Use String.replace(" ",""); for remove spaces after use try-catch for convert.toDouble – erdi yılmaz Nov 07 '19 at 05:04
-
Does this want to check the individual strings between whitespace or the whole string as a whole? This question as it stands is too broad. – RoadRunner Nov 07 '19 at 05:15
-
Why request closing because of off-topic and unclear and too-board? I don't understand. I think it is a good question and I had joy in formulating an answer. This may be usefull. Thanks. – Nov 07 '19 at 10:09
-
https://stackoverflow.com/questions/7461080/fastest-way-to-check-if-string-contains-only-digits – jimjim Nov 08 '19 at 00:01
2 Answers
0
You can use Double.TryParse()
to verify if a string is a valid number, assuming integers and floats/doubles in this case. You will have to split the strings by whitespace beforehand using String.Split()
to check each number individually. You can also utilize Enumerable.All<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
from LINQ to check if all the strings satisfy a condition.
List<string> strings = new List<string> { "1 2 4.5 72", "1 7..5 3.2.1" };
foreach (string item in strings)
{
if (item.Split (new[] { " " }, StringSplitOptions.RemoveEmptyEntries).All (str => double.TryParse (str, out _)))
{
Console.WriteLine ($"{item} has only valid numbers.");
}
else
{
Console.WriteLine ($"{item} does have invalid numbers.");
}
}
// 1 2 4.5 72 has only valid numbers.
// 1 7..5 3.2.1 does have invalid numbers.

Battle
- 786
- 10
- 17

RoadRunner
- 25,803
- 6
- 42
- 75
-
I don't think this was what the OP was trying to ask, I understand that he wants to know if the whole string is a number or a double. so I suggest `string.replace` spaces first before passing – Bosco Nov 07 '19 at 05:07
-
@Bosco This seems a little ambiguous. The OP states that he wants to *"contains only numbers(integers and decimal) with a space in between them*", which means that the string could contain more than one number. Off course one could also interpret it your way as well. He/she needs to clarify this.One of the other answers has also interpreted it my way so its definitely an enquiry to ask. – RoadRunner Nov 07 '19 at 05:10
-
This is the correct answer. However it will get issues if you have 2 spaces next to each other, because empty strings will fail the TryParse and return false. I also corrected the resulting statements. I'll edit the answer. – Battle Nov 07 '19 at 05:54
-
1@Battle Thanks, some very good points. I accepted your edits and suggestions because it improves the answer. – RoadRunner Nov 07 '19 at 05:55
-
Ah sorry, I made a little mistake: The first string example should have multiple spaces (and still be valid), the second one doesn't need to. – Battle Nov 07 '19 at 05:58
-1
you can use split space first then check every split element numberic yes or not by decimal.TryParse + LINQ Any
.
void Main()
{
Console.WriteLine( Check("1 2 4.5 72") ); //true
Console.WriteLine( Check("1 7..5 3.2.1") ); //false
}
bool Check(string text)
{
return text.Split(' ').Any(_ => decimal.TryParse(_, out var num) == false) == false;
}

Wei Lin
- 3,591
- 2
- 20
- 52