-3

I have an string like this:

"ADDRESS  47, 0, 2, 0, 1, 0" 

or

"ADDRESS  47, 100, 2, 5, 1, 0"". 

And now I want to filter the third Number(for example the 2).

I am prorgamming in C#.

Can sameone help me?

Frank
  • 5
  • 1

1 Answers1

0

This code might work:

string input = "ADDRESS 47, 0, 2, 0, 1, 0";
string numbersOnly = input.Substring("ADDRESS ".Length);
string[] parts = numbersOnly.Split(", ");
string result = parts[2];

First, you create a substring that doesn't contain the prefix (not necessary if you are not interested in the first number). After that, you split your string into parts. Chose the part with index 2 to get the third number.

If you want to do calculations with the number, you have to convert it into an integer. Replace the last line with

int result = int.Parse(parts[2]);

Online demo: https://dotnetfiddle.net/txb25A

SomeBody
  • 6,564
  • 2
  • 15
  • 30
  • 2
    You probably want to call `int.Parse(...)` on `parts[2]`. Also, there is no real need to remove the prefix `"ADDRESS "` before splitting, if we're only interested in the third number anyway. – Stef Oct 14 '21 at 09:16