Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Converting string to int and then back to string

I am trying to convert my string variable to an integer to add a value (+1) to it but the result I get is:

1111

Infact I should be getting a total of 4 when I reconvert it to string.

What am I doing wrong?

public string str_Val = "1";

void Update () {
if (str_Val  != "5") {
      str_Val  = int.Parse (str_Val + 1).ToString ();
   }
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

It's all about the priority of the actions:

int.Parse (str_Val + 1)

In the row above first the addition happens str_Val + 1 outputing 11,111,111 etc.

Then the parsing occurs changing "11" to 11

Then to string occurs changing 11 to "11"

So change your code to

str_Val  = (int.Parse(str_Val)+1).ToString();

This will first convert the string to int, then add two integers and finally convert the integer to string again.

over 4 years ago · Santiago Trujillo Report

0

you are concatenating 1 to the string before parsing, that is the reason of the behavior on your code...

so you are doing:

"1" + 1

"11"

Parse to int ("11")

convert to string(11)

do instead:

if (str_Val  != "5")
{
      str_Val  = (int.Parse(str_Val) + 1).ToString ();
   }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!