String Replace

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsrinivasan
    New Member
    • Mar 2007
    • 221

    String Replace

    All,

    I am using c#. I want to replace any special character with space character.

    For eg,
    str = "hello world*& c shar#p"

    should be like this,

    "hello world c shar p"

    thanks,
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Code:
    string s = "Aaa@@@###";
                string[] s2 = new string[] { "*","@","#"};//DEfine special char to remove...
    
                foreach (string s1 in s2)
                {
                    if (s.Contains(s1))
                    {
                      s=  s.Replace(s1, "");
                    }
                }

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Interestingly enough, you don't even have to do the .Contains(). .Replace will replace all instances of the first parameter with the second, and won't throw exceptions if there are none.

      Comment

      • PRR
        Recognized Expert Contributor
        • Dec 2007
        • 750

        #4
        Originally posted by insertAlias
        Interestingly enough, you don't even have to do the .Contains(). .Replace will replace all instances of the first parameter with the second, and won't throw exceptions if there are none.
        lol.. you are right... .Contains is not required.... its just make it more readable....
        Code:
        string s = "@@@";
                    string ss = "AAA@@@";
        
                    ss = ss.Replace("@", "");
                    Console.WriteLine(ss);

        Comment

        • PRR
          Recognized Expert Contributor
          • Dec 2007
          • 750

          #5
          really stupid of me...a lot of times i catch my self doin this... writing code that is not required....

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            Originally posted by dirtbag
            really stupid of me...a lot of times i catch my self doin this... writing code that is not required....
            Alas, a straight replace isn't as handy as you might think as you have to do each individual character separately. It's far easier to use a regular expression.

            VB
            Code:
            Dim OutString As String = Regex.Replace("Hello W\orl#d", "[\#@\*\\/]", " ")
            C#
            Code:
            String OutString = Regex.Replace("Hello World", "[\#@\*\\/]", " ")
            It's not quite as straightforward as a straight replace, but it's far more powerful.

            Lookup Regex Cheat Sheets on Google...

            Comment

            • PRR
              Recognized Expert Contributor
              • Dec 2007
              • 750

              #7
              Thats rights... regular expression is the way to go...even for large log files... use regular expression..... .using strings is usually performance hit.. specially if you are changing the string frequently...
              Theres also "Aho-Corasick algorithm" for even larger files ...

              Peter Bromberg's Article

              For searchin through log files i normally use Regular Expression... havent tried this one(Aho-Corasick algorithm) as yet...

              Comment

              • PRR
                Recognized Expert Contributor
                • Dec 2007
                • 750

                #8
                Originally posted by balabaster
                Alas, a straight replace isn't as handy as you might think as you have to do each individual character separately. It's far easier to use a regular expression.
                VB
                Code:
                Dim OutString As String = Regex.Replace("Hello W\orl#d", "[\#@\*\\/]", " ")
                C#
                Code:
                String OutString = Regex.Replace("Hello World", "[\#@\*\\/]", " ")
                It's not quite as straightforward as a straight replace, but it's far more powerful.

                Lookup Regex Cheat Sheets on Google...
                ok lets consider a case where string has to be matched n replaced...
                which is the best way
                a. If the input string is very small (a few lines)
                b. Its a log file ..few kb
                c. its a large file... very large .. running into mbs...

                Comment

                • balabaster
                  Recognized Expert Contributor
                  • Mar 2007
                  • 798

                  #9
                  Originally posted by dirtbag
                  ok lets consider a case where string has to be matched n replaced...
                  which is the best way
                  a. If the input string is very small (a few lines)
                  b. Its a log file ..few kb
                  c. its a large file... very large .. running into mbs...
                  I've used regex to extract and/or replace strings in files amounting to many hundreds of megabytes... I'd love to know of a more efficient way.

                  The easiest way I could think of is to break the file into the same amount of chunks as there are processors in your machine and have a thread parse each. Make sure there is some overlap so you don't miss any that are broken due to the chunk separation.

                  I guess efficiency comes down to some comp-sci theory about efficient parsing mechanisms.

                  Comment

                  Working...