Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to remove characters from string value

How to remove characters from string value

Scheduled Pinned Locked Moved C#
helptutorial
24 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Henry Minute

    One addition to your code

    string str = "abc, d.e f,g h,i. j,k";
    string\[\] arr = str.Split(',', '.'); // remove all , and .
    foreach (string str1 in arr)
    {
        Console.Write(str1);    
    }
    Console.Write(str);  //<=================== Added line prints original string
    

    What do you get when you run that? The commas and stops are still there. So, once again I say, your code does not remove those characters as the OP asked.

    Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” I wouldn't let CG touch my Abacus!

    R Offline
    R Offline
    RaviRanjanKr
    wrote on last edited by
    #12

    Henry Minute wrote:

    What do you get when you run that?

    yes If I will use your program it will give me Original string but I did programming for removing character and then display it. as i getting OP also what this that's why he greet me for my answer. you are right in your point of view :)

    P 1 Reply Last reply
    0
    • S ShilpaKumari

      Thanks ravi it works for me.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #13

      Although the method will produce the results it is inefficient and causes extra work for you. The String.Replace method is more suitable for the usage you have given.


      I know the language. I've read a book. - _Madmatt

      1 Reply Last reply
      0
      • R RaviRanjanKr

        Henry Minute wrote:

        That doesn't remove them, it just shows them.

        :rolleyes: check my code again I have programmed it and then compile and after compilation it gives me output like "abc de fg hi jk". now what will you say it is removing or showing :^)

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #14

        It may give the appearance of being correct but I don't believe it is what the OP asked for. You are not removing the characters from the string but simply creating an array that excludes them. Now you must recombine the array of characters back into a string to be able to use it.


        I know the language. I've read a book. - _Madmatt

        R 1 Reply Last reply
        0
        • N Not Active

          It may give the appearance of being correct but I don't believe it is what the OP asked for. You are not removing the characters from the string but simply creating an array that excludes them. Now you must recombine the array of characters back into a string to be able to use it.


          I know the language. I've read a book. - _Madmatt

          R Offline
          R Offline
          RaviRanjanKr
          wrote on last edited by
          #15

          yes Mark as Henry pointed me I did big mistake I've not focused my mind on that. now I am realize it was my fault. because of my program OP can only display output after removing character. its not going to remove character from string. its all happen in hurry :(

          1 Reply Last reply
          0
          • S ShilpaKumari

            I want to remove , and . From string value but i don't know how to do that. I h've such type of string value string

            str ="abc, d.e f,g h,i. J,k"

            plz help me

            R Offline
            R Offline
            RaviRanjanKr
            wrote on last edited by
            #16

            My first answer is only able to display value after removing character it will not remove for permanent. you can us Replace Method to remove . and ,

            string str = "abc, d.e f,g h,i. J,k";
            str = str.Replace(',',' ');
            str = str.Replace('.', ' ');
            Console.WriteLine(str);

            1 Reply Last reply
            0
            • R RaviRanjanKr

              Henry Minute wrote:

              What do you get when you run that?

              yes If I will use your program it will give me Original string but I did programming for removing character and then display it. as i getting OP also what this that's why he greet me for my answer. you are right in your point of view :)

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #17

              RaviRanjankr wrote:

              he greet me for my answer

              Yes, and that makes me question his abilities even more.

              1 Reply Last reply
              0
              • S ShilpaKumari

                I want to remove , and . From string value but i don't know how to do that. I h've such type of string value string

                str ="abc, d.e f,g h,i. J,k"

                plz help me

                A Offline
                A Offline
                Abhinav S
                wrote on last edited by
                #18

                Using the Regex class -

                //Getting rid of commas ,
                strComma = ",";
                strReplace = string.Empty;
                Regex Re = new Regex(strComma);
                string newstring = Re.Replace(str,strComma,strReplace);
                //Do the same for .

                The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick

                modified on Sunday, January 23, 2011 11:29 PM

                L 1 Reply Last reply
                0
                • R RaviRanjanKr

                  you can simply remove , and . from your string value by using Split function. take a look how to display

                  string str = "abc, d.e f,g h,i. j,k";
                  string[] arr = str.Split(',', '.'); // remove all , and .
                  foreach (string str1 in arr)
                  {
                  Console.Write(str1); //output abc de fg hi jk
                  }

                  modified on Monday, January 24, 2011 1:04 AM

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #19

                  You can do str = str.Split ( ... ).Join ( ... ), but it's not the right tool and inefficient.

                  1 Reply Last reply
                  0
                  • A Abhinav S

                    Using the Regex class -

                    //Getting rid of commas ,
                    strComma = ",";
                    strReplace = string.Empty;
                    Regex Re = new Regex(strComma);
                    string newstring = Re.Replace(str,strComma,strReplace);
                    //Do the same for .

                    The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick

                    modified on Sunday, January 23, 2011 11:29 PM

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #20

                    Abhinav S wrote:

                    string.Rmpty;

                    Now that is funny. :laugh:

                    I must get a clever new signature for 2011.

                    N A 2 Replies Last reply
                    0
                    • L Lost User

                      Abhinav S wrote:

                      string.Rmpty;

                      Now that is funny. :laugh:

                      I must get a clever new signature for 2011.

                      N Offline
                      N Offline
                      Not Active
                      wrote on last edited by
                      #21

                      string.Rmpty is perfectly acceptable syntax with the Scooby-Do extensions installed. :-D


                      I know the language. I've read a book. - _Madmatt

                      1 Reply Last reply
                      0
                      • L Lost User

                        Abhinav S wrote:

                        string.Rmpty;

                        Now that is funny. :laugh:

                        I must get a clever new signature for 2011.

                        A Offline
                        A Offline
                        Abhinav S
                        wrote on last edited by
                        #22

                        Is fixed now.

                        The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick

                        1 Reply Last reply
                        0
                        • S ShilpaKumari

                          I want to remove , and . From string value but i don't know how to do that. I h've such type of string value string

                          str ="abc, d.e f,g h,i. J,k"

                          plz help me

                          P Offline
                          P Offline
                          Paladin2000
                          wrote on last edited by
                          #23

                          Try this.

                          string str = "abc, d.e f,g h,i. J,k";
                          str = str.Replace(',', string.Empty).Replace('.', string.Empty);

                          1 Reply Last reply
                          0
                          • R RaviRanjanKr

                            you can simply remove , and . from your string value by using Split function. take a look how to display

                            string str = "abc, d.e f,g h,i. j,k";
                            string[] arr = str.Split(',', '.'); // remove all , and .
                            foreach (string str1 in arr)
                            {
                            Console.Write(str1); //output abc de fg hi jk
                            }

                            modified on Monday, January 24, 2011 1:04 AM

                            R Offline
                            R Offline
                            RaviRanjanKr
                            wrote on last edited by
                            #24

                            I have done Minor changes in my answer

                            string str = "abc, d.e f,g h,i. j,k";
                            string[] arr = str.Split(',', '.'); // remove all , and .
                            str = string.Empty; // Changes here
                            foreach (string str1 in arr)
                            {
                            str += str1;
                            }
                            Console.WriteLine(str); //output abc de fg hi jk

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups