locked
how to remove character from strings RRS feed

  • Question

  • User1122836778 posted

    i have two string and one array of string.
    let the str1, str2, strArr
    here

          str1="1";
          strArr elements are {2,5,7}
          str2="1,3,4,5,6,1,3,7,2,1,5,6";
    now i want remove those char from str2 which are present in strArr, and str1
    Note: str1 will always have 1 char, but strArr can have any number of elements.

     

    Wednesday, July 23, 2014 2:11 PM

Answers

  • User281315223 posted

    There are quite a few ways to handle this, however you would likely want to use the following :

    1. Merge your str1 and all of the values of your strArr into a single collection
    2. Convert your existing str2 string into a collection of values using the String.Split() method so you can easily access all of the individual values.
    3. Use the LINQ Except() method to remove any characters present in your first collection (str1 and strArr)
    4. Use the String.Join() method to recreate your original string.

    You can see a working example below :

    // Your original values
    string str1="1";
    string[] strArr = new string[]{"2","5","7"};
    string str2= "1,3,4,5,6,1,3,7,2,1,5,6";
    
    // Merge your strArr and str1
    var mergedValues = strArr.Concat(new List<string>(){ str1}); // yields { "1", "2", "5", "7" }
    		
    // Split your str2 string into a collection of values
    var str2Values = str2.Split(new char[]{ ','}, StringSplitOptions.RemoveEmptyEntries); // yields { "1", "3", "4", "6", "1", "3", "7", "2", "1", "5", "6" }
    		
    // Remove all values that are present in your mergedValues collection from str2
    str2Values = str2Values.Except(mergedValues).ToArray(); // yields { "3", "4", "6" }
    		
    // Output your updated string
    var updatedStr2 = String.Join(",",str2Values); // yields "3,4,6"
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 23, 2014 2:55 PM

All replies

  • User-1360095595 posted

    Add str1 to strArr and then use LINQ Except() on str2 & strArr. Easy and simple. 

    Refer: http://msdn.microsoft.com/en-us/library/vstudio/bb300779(v=vs.100).aspx

    Edit: corrected str1 to strArr.

    Wednesday, July 23, 2014 2:21 PM
  • User281315223 posted

    There are quite a few ways to handle this, however you would likely want to use the following :

    1. Merge your str1 and all of the values of your strArr into a single collection
    2. Convert your existing str2 string into a collection of values using the String.Split() method so you can easily access all of the individual values.
    3. Use the LINQ Except() method to remove any characters present in your first collection (str1 and strArr)
    4. Use the String.Join() method to recreate your original string.

    You can see a working example below :

    // Your original values
    string str1="1";
    string[] strArr = new string[]{"2","5","7"};
    string str2= "1,3,4,5,6,1,3,7,2,1,5,6";
    
    // Merge your strArr and str1
    var mergedValues = strArr.Concat(new List<string>(){ str1}); // yields { "1", "2", "5", "7" }
    		
    // Split your str2 string into a collection of values
    var str2Values = str2.Split(new char[]{ ','}, StringSplitOptions.RemoveEmptyEntries); // yields { "1", "3", "4", "6", "1", "3", "7", "2", "1", "5", "6" }
    		
    // Remove all values that are present in your mergedValues collection from str2
    str2Values = str2Values.Except(mergedValues).ToArray(); // yields { "3", "4", "6" }
    		
    // Output your updated string
    var updatedStr2 = String.Join(",",str2Values); // yields "3,4,6"
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 23, 2014 2:55 PM