• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

Finding an integer number in a string surrounded by spaces

Hello Experts,

I need a function that returns a true if a string contains an integer.  But it has to be a true number, no alpha numerics.

example string 1: "This is a string with a number: 123456 so that's true?"
example string 2: "This string has alpha-numeric data: B123456D so that's false?"

So essentially an integer surrounded by spaces.

Thanks!
Moe
0
mauricerichard
Asked:
mauricerichard
  • 2
  • 2
2 Solutions
 
käµfm³d 👽Commented:
Is the answer you received in your previous question not working?
0
 
Meir RivkinFull stack Software EngineerCommented:
bool IsContainsInteger(string value){
foreach(var str in value.Split(' ')){
int tmp;
if(int.TryParse(str, out tmp))
return true;
}
return false;
}
0
 
käµfm³d 👽Commented:
Never mind. I see what you mean now. You can simply add word boundaries to the answer I gave you in the other question:

string number = Regex.Match(source, @"\b\d+\b").Value;

Open in new window

0
 
mauricerichardAuthor Commented:
Thanks for the quick reply everyone, I'll try these solutions!
0
 
mauricerichardAuthor Commented:
There were both useful to me, thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now