I have taken from the clipboard copied from the browser 194 and it is not converted, writes

A raw exception of type "System.FormatException" in mscorlib.dll Additional Information: The input string had an invalid format.

What to do?

string CS = Clipboard.GetText(); int css = Convert.ToInt32(CS); 
  • How do you convert? Code? - andreycha
  • string CS = Clipboard.GetText (); int css = Convert.ToInt32 (CS); - Alexander
  • Add it to the question and use the code formatting function, please. - andreycha
  • 2
    Your code is working - "194" is easily converted into a number. Look in the debugger for what is in the CS variable. - andreycha
  • one
    @ Alexander: Then this is not a number. You have a string of text, and you need to pull a number out of it. These are completely different tasks. - VladD

1 answer 1

As noted by the author of the message in the comments, the number in the buffer is not in pure form => it must be separated from the text.

If the buffer can always have only 1 number, then you can use a regular of the form \d+ and convert the result of the regular expression to int

You can also use standard string functions if you know that the number comes after ':'

  • It will not work if the text goes, say, 100 digits in a row. - VladD
  • @VladD, Well, in any case, it depends on what the author expects in the buffer. The main idea is to bring the string in the buffer to a normal form, which would then be cast. - iluxa1810 September
  • Well yes. I actually understand the problem by the author of the question. - VladD