1

There are three kind of strings:

  • string(1)_2013-02-15
  • anotherString(2)_2013-02-15
  • yetAnotherString(3)_2013-02-15

Up to the opening parenthesis each string is constant. The number between the parenthesis will change and it can be one, two or three characters in length for all three strings. The date would be the current day's date.

I want to remove the parenthesis and the number between them. The desired result is as follows:

  • string_2013-02-15
  • anotherString_2013-02-15
  • yetAnotherString_2013-02-15

Any help would be appreciated.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Ozzyberto
  • 379
  • 6
  • 13
  • 2
    http://stackoverflow.com/questions/1359412/c-sharp-remove-text-in-between-delimiters-in-a-string-regex `\((.*?)\)` – Stan Feb 15 '13 at 22:26
  • 1
    You could use Regex.Replace() with a pattern like "\\(\d+\\)" and replace it with an empty string. – lintmouse Feb 15 '13 at 22:28

7 Answers7

2
string MyString = "string(1)_2013-02-15";
int firstParenIndex = MyString.IndexOf("(");
int secondParenIndex = MyString.IndexOf(")");
string String1 = MyString.Substring(0,firstParenIndex);
string String2 = MyString.Substring(seconParenIndex+1);
string finalString = String1 + String2;
Melanie
  • 3,021
  • 6
  • 38
  • 56
2

With plain String methods:

int leftBrace =  str1.IndexOf('(');
int rightBrace = str1.IndexOf(')', leftBrace);
str1 = str1.Remove(leftBrace, rightBrace - leftBrace + 1);

DEMO

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
2
var input = "string(1)_2013-02-15";
var result = Regex.Replace(input, "\\([0-9]+\\)", string.Empty);
trydis
  • 3,905
  • 1
  • 26
  • 31
  • Plain and simple. I like it! – Ozzyberto Feb 15 '13 at 23:02
  • +1 This is the safest and most readable solution I think, it only matches numbers in between parenthesis. The `IndexOf` solutions, although faster, would match any parenthesis pair regardless of their position. – C.Evenhuis Feb 15 '13 at 23:15
1

You could use string.split('(') and remove that way, for example if you know there is only one set of brackets:

public string RemoveBrackets(string Message)
{
    string temp1 = Message.Split('(')[0];
    string temp2 = Message.Split(')')[1];
    string newMessage = temp1 + temp2;
    return newMessage;
}

Something like that, anyway :)

XtrmJosh
  • 889
  • 2
  • 14
  • 33
0

Try this:

int index1 = test2.IndexOf('(');
int index2 = test2.LastIndexOf(')', index1 + 1);
string result2 = test2.Remove(index1, index2 - index1);
Linuxios
  • 34,849
  • 13
  • 91
  • 116
shenku
  • 11,969
  • 12
  • 64
  • 118
0

Considering constant length of date part till year 9999 which is far enough, you can optimize your code by reducing one call (finding the position of the second parentheses as below:

string s = "something(123)_2013-02-16";
string result = s.Substring(0, s.IndexOf("(")) + s.Substring((s.Length - 11), 11);
Xaqron
  • 29,931
  • 42
  • 140
  • 205
0

Using Substring() and LastIndexOf() method works even if first part of the string contains ( and/or ).

string s = "string(1)_2013-02-15";
s = s.Substring(0, s.LastIndexOf('(')) + s.Substring(s.LastIndexOf(')')+1);
Kaf
  • 33,101
  • 7
  • 58
  • 78