Converting a string to an Int

Okay, I’m having a problem trying to figure out how to convert a string to an int… My code in the game is a little too large to show, so I’m just going to make one…

We have a server that reads the highscore as a string, and then displays the leaderboards on the game as a string. But what we want is when a player has reached another players’ highscore, a clone of the other player will instantiate to show the servers highscore. This requires the game to read that highscore as an int.

Say I have a string, like so:

private string s = "Name 500";

How would I convert that to an int?

Why not separate name and 500 and the just use an int, convert to string?

Private int score = 500;
Private string s = "name" + score.ToString();

I tend to use Java so, my syntax may be off.

I agree with renman. You will need to separate the data.

The easiest way to convert an string to an int is:

int a = (int)string b;

but it will still give a bug as your string also contains an name and an string of letters can’t be converted to an int only numbers can be converted to an int. There is a work around of course but that would mean you would first remove the name part from the string and I don’t think that is what you want.

so easiest would be to do as remnan3000 told you and that is to separate the score and the name.

        char[] tmp = new char[] { ' ' };
        string[] strings = s.Split(tmp);
        int result = int.Parse(strings[1]);
        Debug.Log(result);

s is your string, result is the int from the string

Renman, I could do that, but they are being fed from the server. It all comes into the game as one string… So, I would still have to separate from the string no matter if I separated their types or not. I think appels may have found the correct answer. I’m going to implement it now and tell you if it works.

I’m Epictickles friend.
The problem we’re facing is that it’s taking it as a string, with spaces and linebreaks.
For example
“John 200
Steve 15”
So when I split it I get stuff like
“200
Steve”
In one array index.

More specifically, where sTemp is the reading of the highscore

string[] lines = Regex.Split(sTemp, "	");
			
			foreach (string line in lines){
	    		print(line);

this prints:
John

1122
John

201
Jane

16
John

I separated them with a line so you could see each index of the array. I’m going to try what was suggested here.

The delimiter used here is a space :

char[] tmp = new char[] { ' ' };

You should use a well defined delimiter like this :

char[] tmp = new char[] { '|' };

Then you send your data like this :
string s = “Player x|250”;
Names may have spaces.
You can also trim unwanted spaces if needed.

Thanks for the reply. I should clarify that it’s actually not a space, it’s a weird ascii character that’s closer to a tab. My clipboard keeps converting it to a space.
It’s based off this script http://wiki.unity3d.com/index.php?title=Server_Side_Highscores
with http://forum.unity3d.com/threads/106392-String-parse-error-in-high-score-script this

I don’t know… Would be much too complicate for me. Set the strings, split them on get, make a simple “for” routine and dash it all in a gui.

No, I got everything to work fine now… Except I keep getting an annoying FormatException: Input string was not in the correct format, on line 121.

Here is the code:

IEnumerator GetScores()
    {
		//this.gameObject.guiText.text="Loading Scores";
        //gameObject.guiText.text = "Loading Scores";
		print ("Loading Scores");
        WWW hs_get = new WWW(highscoreURL);
        yield return hs_get;
		
		int index=0; // Index of my loop
		print(HighNumbers.Length);
		string sTemp=hs_get.text; // putting the highscore text into a string called sTemp
		string[] lines = Regex.Split(sTemp, " "); // Splitting it based on spaces - should probably be changed to | but fuck it i'll do that later
			 
    	
			foreach (string line in lines){
			int i;
			HighNumbers[index]=int.Parse(lines[index]); // putting the content of line (which was split based on spaces) into index of HighNumbers
			
			print(HighNumbers[index]+" "+index); 
			index++;
			ScoresLoaded = true;
			
			
		}

I parsed everything, and they all now return into an array, which I can see in the inspector… I’m just annoyed with the Exception… Any ideas?

Edit: The error comes from line 17 in this code, I didn’t post the whole script.

Well you split at string with both letters and numbers like “Letter 500” right? Then you go through it all with your foreach loop, and thereby also cast “Letter” which wil give you the expection.

To avoid it you could use int.TryParse, which will return false if it cant be cast. In this manner you can sort the wrong stuff.

int numberWeCheck;
bool checkIfCasted = int.TryParse(string1, out numberWeCheck);

If it is casted then it will set numberWeCheck to the string (out is a reference to the variable)