D
D
Dmitry2018-07-05 14:39:00
C++ / C#
Dmitry, 2018-07-05 14:39:00

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

2 answer(s)
V
Victor P., 2018-07-05
@Jeer

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));
    }
}

call anywhere,
var C_formula = "C10";
var res = C_formula.atoi();

G
Griboks, 2018-07-05
@Griboks

Do you want a dll that has a parseCformula() function? I can send you one.
Want a low level thing? Not a problem. List everything in order.
Want pasta code?
Don't like long lines? Turn on auto transfer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question