Remove specific char in string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maylortaylor
    New Member
    • Nov 2012
    • 72

    Remove specific char in string

    Code:
    int index = -1;
                    string NewStr = null;
                    char[] lower = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
                    char[] upper = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    
                    foreach (char c in str)
                    {
                        
                        index = str.IndexOfAny(lower);
                        if (index == -1)
                        {
                            index = str.IndexOfAny(upper);
                        }                    
                            NewStr = str.Remove(index, 1);                   
                    }
    The code above loops through a string and for each character checks to see if it is a lower case character, then checks to see if it an upper case. It then removes it if it is. Leaving only the numbers.

    However, it reuses the same string the entire time, never updating the string so it always finds the same (first) character.

    Any help on figuring this out?
  • maylortaylor
    New Member
    • Nov 2012
    • 72

    #2
    ANSWERED MYSELF:

    Code:
    int index = -1;
                    Boolean flag = false;
                    string NewStr = null;
                    char[] lower = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
                    char[] upper = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    
                    foreach (char c in str)
                    {
                        if (flag == true)
                        {
                            str = NewStr;
                        }
                        index = str.IndexOfAny(lower);
                        if (index == -1)
                        {
                            index = str.IndexOfAny(upper);
                            
                        }
                        if (index != -1)
                        {
                            NewStr = str.Remove(index, 1);
                            flag = true;
                        }
                    }

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Leaving only the numbers.
      That's not what the code does by the way. It leaves everything but letters. If you truly want to leave only numbers, then you need to whitelist the numbers, not blacklist the letters. With the current code, if there is incorrect input with letters, numbers, AND symbols, you'll be left with numbers and symbols.

      Comment

      • maylortaylor
        New Member
        • Nov 2012
        • 72

        #4
        stuffing!!!..yo u are right. Welp, back to the drawing board. Any pseudo code help?

        Comment

        • maylortaylor
          New Member
          • Nov 2012
          • 72

          #5
          Code:
          char[] whitelist = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
          
                          
                             
                              for (int i = 0; i < whitelist.Count(); i++)
                              {
                                  foreach (char c in str)
                                  {
                                      if (whitelist.Contains(c))
                                      {
                                          NewStr += str.Substring(i, 1);
                                      }
                                  }
                              }
          Well I have this code now, that uses a whitelist of numbers. However, i'm having trouble figuring out the for loops and how to properly configuring them.

          Comment

          • maylortaylor
            New Member
            • Nov 2012
            • 72

            #6
            Find this online and it did exactly what I wanted

            Code:
            using System.Text.RegularExpressions;
            ...
            string newString = Regex.Replace(oldString, "[^.0-9]", "");

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              Glad you found a solution, good luck on the rest of your project.

              Comment

              Working...