Max
Max

Reputation: 13338

Filter begin and end characters from a string

I receive the following string every time I open the serial port, this string is for checking which bios version and which firmware version the connected device is running.

String:

v03400216000000001FC5H240108206B-RAMBOX=[03]

Now the only important charachters for checking the Bios version and Firmware version are the following:

03400216

Note: bytearray is a byte[] converted to a string and is filled with:

v03400216000000001FC5H240108206B-RAMBOX=[03]

I tried achieving this using the following code:

string versiontext = bytearray.Trim();
bytearray = versiontext.Remove(0, 2);
versiontext = bytearray.Remove(9, bytearray.Length);
bytearray = versiontext;

But that didn't work out and resulted in the following exception:

Index and count must refer to a location within the string.

Should I use a Regex, Instead of the .Remove?

EDIT:

@lazyberezovsky Helped me out, I just forgot about the .Substring().

I achieved the string result with the following code:

if (bytearray.Contains("RAMBOX"))
{
       string Versionstring = bytearray.Substring(1, 8);
       bytearray = Versionstring;
}

Upvotes: 3

Views: 253

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236218

What about simple substring?

var input = "v03400216000000001FC5H240108206B-RAMBOX=[03]";
var version = input.Substring(1, 8);

Upvotes: 6

Related Questions