Cannot convert string to integer

Hi all,

I’m facing a issue when I try to convert string to integer:

1/ first of all, I get my value from a webpage. Always number display as “3 183” or “123 719”. Information is stored in var1 (generic).
I need to convert my var1 in order to compare it to an other variable that come from Excel (without blank).

2/ I try to remove the blank by using replace method: var1.ToString.Replace(" “,”"), but the blank character is still there
In addition, I try to convert directly by Convert.ToInt32(var1.ToString), but I get error message : “Input string was not in a correct format”

I think my var1 contains special character…

Do you have any ideas ?

Thanks for your help

Florian

See this -

Main.xaml (5.7 KB)

Have you logged the variable immediately after it is read from the webpage? The logs could show a hidden character.

1 Like

hi @flor1an

use this

Convert.ToInt32(tst.ToString.Replace(" “,”").Trim).ToString

It might help update on this

Regards

1 Like

Both suggestions doesn’t work :frowning_face:

  • cINT(str.Replace(" “,”")).ToString ==> Error message: Conversion from string “9 985” to type ‘Integer’ is not valid.
  • Convert.ToInt32(tst.ToString.Replace(" “,”").Trim).ToString ==> Error message: Input string was not in a correct format

@KEntwistle, nothing in particular appears in my log : “9 985”

I tried without success var1.ToString.Replace(" “,”")

thanks to your help

Hope this may help you

string_val=“3 183”
Convert.ToInt32(string_val.Replace(" ",string.Empty))

Regards,
Pathrudu

cint or convert.toint32 seem like they would still work here, the issue is that you’re not removing the blank space in the number.

If pathrudu’s method of string.replace(" “,string.empty) isn’t working, you could also use regex.replace(yourString,”[\D]“,”")

2 Likes

I just noticed that you’re storing var1 as type Generic and converting var1 in place. You should instead have 2 variables used. Var1 should be string and Var2 should be type integer.

Get Var1 from the webpage e.g. “3 183”. Now assign Var2 = CInt(Var1.Replace(" “,”"))

Converting the type in place while it is “Generic” is what is causing your issues

Thanks for your help.

The regex.replace(yourString,”[\D]“,”") method solves my problem :smile: :smile: :smile:

1 Like