[Solved] C# editing string


Step one: use var arr = string.Split(new string[] {"."}, StringSplitOptions.None) to get an array of strings. arr[2] should contain your number.

Step two: use Convert.ToInt32() to convert this string to an int.

Step 3: increase / decrease this int by two

Step 4: Write the number back to the were it was saved in the first place arr[2] = number.ToString();

Step 5: use string.Join() to join the strings back together

EDIT

My explanations might be a bit unclear, here the full code:

var list = new List<string>() { "192.168.1.1" };
var arr = list[0].Split(new string[] { "." }, StringSplitOptions.None);
var number = Convert.ToInt32(arr[2]);
number += 2;
arr[2] = number.ToString();
var result = string.Join(".", arr);

1

solved C# editing string