Remove the comma between the [] brackets in the line using C #

I want to remove the comma between the square brackets [] instead of the integer commas from the string.

Here is my line,

  string result = "a, b, c, [c, d, e], f, g, [h, i, j]"; 

Expected Result:

  a, b, c, [cde], f, g, [hij] 

Thanks in advance.

+4
source share
3 answers

As I already wrote, you need a simple state machine (inside brackets, external brackets) ... Then for each character you analyze it, and if necessary, you change the state of the state machine and decide whether you need to output it or not.

public static string RemoveCommas(string str) { int bracketLevel = 0; var sb = new StringBuilder(str.Length); foreach (char ch in str) { switch (ch) { case '[': bracketLevel++; sb.Append(ch); break; case ']': if (bracketLevel > 0) { bracketLevel--; } sb.Append(ch); break; case ',': if (bracketLevel == 0) { sb.Append(ch); } break; default: sb.Append(ch); break; } } return sb.ToString(); } 

Use it as:

 string result = "a,b,c,[c,d,e],f,g,[h,i,j]"; Console.WriteLine(RemoveCommas(result)); 

Note that I use int to “save” the state of the state machine, so it works with recursive brackets, for example a,b,[c,d,[e,f]g,h]i,j

As an interesting exercise, this can be done with a slower LINQ expression:

 string result2 = result.Aggregate(new { BracketLevel = 0, Result = string.Empty, }, (state, ch) => new { BracketLevel = ch == '[' ? state.BracketLevel + 1 : ch == ']' && state.BracketLevel > 0 ? state.BracketLevel - 1 : state.BracketLevel, Result = ch != ',' || state.BracketLevel == 0 ? state.Result + ch : state.Result }).Result; 

At the end, the code is very similar ... There is a state that is taken ( BracketLevel ) plus a line ( Result ) that is being built. please do not use it , it is written only as a fun LINQ snippet.

+4
source

Regex approach

 string stringValue = "a,b,c,[c,d,e],f,g,[h,i,j]"; var result = Regex.Replace(stringValue, @",(?![^\]]*(?:\[|$))", string.Empty); 

if you don't have parentheses

+2
source

You can try the following:

 var output = new string(result .Where((s, index) => s != ',' || IsOutside(result.Substring(0, index))) .ToArray() ); //output: a,b,c,[cde],f,g,[hij] 

AND

 private static bool IsOutside(string value) { return value.Count(i => i == '[') <= value.Count(i => i == ']'); } 

But remember, this is not an effective way to accomplish this task.

0
source

All Articles