- Home /
parseInt givin' me issues!
So I extract a string, by taking [string] input then .Remove(a,b)and remove part of the string, this leaves me with a string. Now I want to compare the left part of this string to an integer, eg. String: "Render 100" -> String: "100" -> ParseInt -> Int: 100
But upon entering "Render 100a" this results in an error, 100a cannot be parsed to int. How do I avoid this, possibly in the part where I compare the original string, and checks if it has an index of "Render".
Answer by jogo13 · Dec 30, 2012 at 09:42 PM
ParseInt cannot understand the 'a' at the end of '100a.' You'll need to remove it. (Why is it there?) //answer in comment below
Run it through your own 'preprocess' to create a string of only digits 0-9:
string original_string;
string number_string = "";
for(int i = 0; i < original_string.Length; i++)
{
//continue this line from 0-9
if(original_string[i] == '0' || original_string[i] == '1' || original_string[i] == '2'...)
{
//is a digit, add to string
//is a char, need to convert to string
number_string += original_string[i].ToString();
}
else //not a digit
{
//do nothing for now
}
}
//number_string should now be a string of only the numbers from the original_string
//There's probably a more elegant way to do this :)
No I realize that, but it's a consequence of user input, I can't remove the n'th ltter, since it might be 10, 10a, 100aaa.. Various output, I need a way to sort any int out of a string.
Can use char.isDigit( W[i] )
ins$$anonymous$$d of checking 0-9 by hand.
Code above would read "4a6b1c" as 461. To grab only continuous numbers, set sawNum=false;
. When you see a number, set it true, so you know are reading digits. If sawNum=true and you don't see a number quit the loop (break.)
But, might be better to have the input method just reject letters (if you type one, it won't show up, like some internet forms.) Give the user faster feedback.
Answer by Bunny83 · Dec 31, 2012 at 01:14 AM
https://www.google.com/#q=c%23+extract+number+string
first result -->
http://stackoverflow.com/questions/4734116/c-sharp-find-and-extract-number-from-a-string
edit
Or even just: that which gives me this:
http://answers.unity3d.com/questions/19053/extract-number-from-string.html
I sometimes go google the answer to the questions posted here. Almost allways there are answers in 9/10 top links. $$anonymous$$any from unity answers itself. People don't even try no more.
Your answer
Follow this Question
Related Questions
Convert Array to String 3 Answers
Is it possible for parseInt("5+5") to return 10? 2 Answers
Convert Text to float 3 Answers
Why Won't This Work? 1 Answer