Answer the question
In order to leave comments, you need to log in
Converting string to int with ignoring characters without unnecessary gestures?
Hello!
Please tell me if there are methods in the VS C# set for converting string to int
that works similarly to atoi , that is, it ignores all characters except digits and gives a number without whims at the output.
I have a bunch of hydrocarbons like C4 C10 and others ... I need to choose the right ones from their set, I want to compare the numerical values after C. Yes, I can cut the string and get a string with only numbers and run it through int.Parse().
But I really like atoi in such cases. I want to find something here as well.
I’ve been sitting in sharps recently, and so far I haven’t been able to find anything like this, write my own function ... Well, even if it’s not sporty, there should be a “native” function in the studio, that’s for everyone)))))
Answer the question
In order to leave comments, you need to log in
I can suggest extension methods)
https://metanit.com/sharp/tutorial/3.18.php
public static class StringExtension
{
public static int atoi(this string str)
{
return int.Parse(str.Substring(1));
}
}
var C_formula = "C10";
var res = C_formula.atoi();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question