Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda