Manipulating strings in C # to increase the number

I am using .net 2.0 and I have the following line

string 1 = "test (10)" 

I have a regex that removes the number 10 from a string, then I want to increment it and insert in the parentheses of the previous line to create new lines like this:

 string 2 = "something else(11)" 
+4
source share
5 answers

If the string will always have the same format, you can do it like this:

 int myNumber = 11; string two = String.Format("test ({0})", myNumber); 

It is assumed that you already have RegExp, as you say in your question, and increased it by 1.

EDIT

A new example according to your new information:

 int myNumber = 11; int myNewString = "Test"; string two = String.Format("{0} ({1})", myNewString, myNumber); 
+2
source

I don't know what you want to do ... but KEEP IT SIMPLE

 var value = ExtractValue(str1); value++; string str2 = "test (" + value + ")"; 
0
source

Let the class handle this ... something like this.

TestNumbers testNumbers = new TestNumbers (1); testNumbers.Increment (1); testNumbers.GetNumber (); testNumbers.ToString ();

  public class TestNumbers { private int number = 0; public TestNumbers(int number) { this.number = number; } public override string ToString() { return "Test (" + number + ")"; } public void Increment(int incrementStep) { number += incrementStep; } public int GetNumber() { return number; } } 
0
source

you use the following code:

 String s1 = string.format("{0} ({1})",yourstring,value); 
0
source

A simple regex solution could be:

 string inputString = "test (10)"; Regex regex = new Regex(@"(.+)\((?<Digit>\d+)\)"); Match match = regex.Match(inputString); int i = Convert.ToInt32(match.Groups["Digit"].Value); i++; string replacePattern = "$1(" + i + ")"; string newString = regex.Replace(inputString, replacePattern); Console.WriteLine(newString); 
0
source

Source: https://habr.com/ru/post/1337760/


All Articles