0

I have a string

"[\"1,1\",\"2,2\"]"

and I want to turn this string onto this

1,1,2,2

I am using Replace function for that like

obj.str.Replace("[","").Replace("]","").Replace("\\","");

But it does not return the expected result. Please help.

dtb
  • 198,715
  • 31
  • 379
  • 417
Toseef Khilji
  • 16,458
  • 10
  • 78
  • 115

5 Answers5

5

You haven't removed the double quotes. Use the following:

obj.str = obj.str.Replace("[","").Replace("]","").Replace("\\","").Replace("\"", "");
rcs
  • 5,312
  • 9
  • 45
  • 61
2

Here is an optimized approach in case the string or the list of exclude-characters is long:

public static class StringExtensions
{
    public static String RemoveAll(this string input, params Char[] charactersToRemove)
    {
        if(string.IsNullOrEmpty(input) || (charactersToRemove==null || charactersToRemove.Length==0))
            return input;

        var exclude = new HashSet<Char>(charactersToRemove); // removes duplicates and has constant lookup time
        var sb = new StringBuilder(input.Length);
        foreach (Char c in input)
        {
            if (!exclude.Contains(c))
                sb.Append(c);
        }
        return sb.ToString();
    }
}

Use it in this way:

str = str.RemoveAll('"', '[', ']', '\\'); 
// or use a string as "remove-array":
string removeChars = "\"{[]\\";
str = str.RemoveAll(removeChars.ToCharArray());
Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
  • since this essentially what I wrote, in response to your comment, +1 – Jodrell Sep 25 '13 at 09:50
  • theres another style cop rule that says use (in effect) use `char` instead of `Char`. – Jodrell Sep 25 '13 at 09:53
  • @Jodrell: I have a feeling that Jon Skeet doesn't use style cop ;-) [_" I use the aliases everywhere for the implementation"_](http://stackoverflow.com/a/215422/284240) I personally also don't use the uppercase class names for the classes with aliases just because i'm lazy. – Tim Schmelter Sep 25 '13 at 10:04
1

You should do following:

obj.str = obj.str.Replace("[","").Replace("]","").Replace("\"","");

string.Replace method does not replace string content in place. This means that if you have string test = "12345" and do

test.Replace("2", "1");

test string will still be "12345". Replace doesn't change string itself, but creates new string with replaced content. So you need to assign this new string to a new or same variable

changedTest = test.Replace("2", "1");

Now, changedTest will containt "11345".

Another note on your code is that you don't actually have \ character in your string. It's only displayed in order to escape quote character. If you want to know more about this, please read MSDN article on string literals.

Nikola Radosavljević
  • 6,773
  • 29
  • 44
1

how about

var exclusions = new HashSet<char>(new[] { '"', '[', ']', '\\' });
return new string(obj.str.Where(c => !exclusions.Contains(c)).ToArray());

To do it all in one sweep.

As Tim Schmelter writes, if you wanted to do it often, especially with large exclusion sets over long strings, you could make an extension like this.

public static string Strip(
        this string  source,
        params char[] exclusions)
{
    if (!exclusions.Any())
    {
        return source;
    }

    var mask = new HashSet<char>(exclusions);
    var result = new StringBuilder(source.Length);
    foreach (var c in source.Where(c => !mask.Contains(c)))
    {
        result.Append(c);
    }

    return result.ToString();
}

so you could do,

var result = "[\"1,1\",\"2,2\"]".Strip('"', '[', ']', '\\');
Jodrell
  • 31,518
  • 3
  • 75
  • 114
  • `Except` uses a set and removes all repeating characters from both sequences, hence the resulting array does not contain duplicate characters which is probably not desired. If you want to avoid multiple `Replaces` the best way is to use a loop and a `StringBuilder`. – Tim Schmelter Sep 25 '13 at 09:12
  • Should work (if you change it to `...new []{...`). But i doubt that it's more efficient at all because of the `ToArray()`. – Tim Schmelter Sep 25 '13 at 09:36
  • @TimSchmelter, damn typos. – Jodrell Sep 25 '13 at 09:45
0

Capture the numbers only with this regular expression [0-9]+ and then concatenate the matches:

var input = "[\"1,1\",\"2,2\"]";
var regex = new Regex("[0-9]+");
var matches = regex.Matches(input).Cast<Match>().Select(m => m.Value);
var result = string.Join(",", matches);
Karel Frajták
  • 4,409
  • 20
  • 33