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. Deleting words from string

Deleting words from string

Scheduled Pinned Locked Moved C#
question
17 Posts 7 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.
  • K KaineDunno

    what you could do is keep it simple :) use string.Replace. ex: string x = "This is a test,test"; string y = x.Replace("test",""); Console.WriteLine(y); output should look like "This is a , " have fun Kaine

    A Offline
    A Offline
    Anthony Mushrow
    wrote on last edited by
    #4

    Damn, string.Replace. How could i forget that. Oh well, time to go methinks. On a side note, i appear to have found a new forum to live on for a while. Who would have thought it'd be here?

    My current favourite word is: Waffle Cheese is still good though.

    1 Reply Last reply
    0
    • A Anthony Mushrow

      You can use split, like this: string myString = "this is a test waffle"; string temp[] = myString.Split("test "); myString = ""; foreach(string str in temp) { myString += str; } Then, after all that, your string should be "this is a waffle".

      R Offline
      R Offline
      Ryno Burger
      wrote on last edited by
      #5

      Thanks to both of you that replied!! R

      R 1 Reply Last reply
      0
      • R Ryno Burger

        Thanks to both of you that replied!! R

        R Offline
        R Offline
        Ryno Burger
        wrote on last edited by
        #6

        But what if you were to remove a word like '{test}' including the braces?

        R 1 Reply Last reply
        0
        • R Ryno Burger

          But what if you were to remove a word like '{test}' including the braces?

          R Offline
          R Offline
          Ryno Burger
          wrote on last edited by
          #7

          Nevermind I got it now, I'm just being stupid today... Thanks again guys!

          1 Reply Last reply
          0
          • A Anthony Mushrow

            You can use split, like this: string myString = "this is a test waffle"; string temp[] = myString.Split("test "); myString = ""; foreach(string str in temp) { myString += str; } Then, after all that, your string should be "this is a waffle".

            R Offline
            R Offline
            Ryno Burger
            wrote on last edited by
            #8

            Is there a neater more professional way to re-write the code below? public string StripString(string statementString) { string alteredStatement = string.Empty; string keyword1 = "[dbo]."; string keyword2 = "["; string keyword3 = "]"; string remover = ""; string tmpStr1 = statementString.Replace(keyword1, remover); string tmpStr2 = tmpStr1.Replace(keyword2, remover); alteredStatement = tmpStr2.Replace(keyword3, remover); return alteredStatement; }

            K A J 3 Replies Last reply
            0
            • R Ryno Burger

              Is there a neater more professional way to re-write the code below? public string StripString(string statementString) { string alteredStatement = string.Empty; string keyword1 = "[dbo]."; string keyword2 = "["; string keyword3 = "]"; string remover = ""; string tmpStr1 = statementString.Replace(keyword1, remover); string tmpStr2 = tmpStr1.Replace(keyword2, remover); alteredStatement = tmpStr2.Replace(keyword3, remover); return alteredStatement; }

              K Offline
              K Offline
              KaineDunno
              wrote on last edited by
              #9

              Do you mean something like this? Regex.Replace(statementString, @"\[|dbo].|\]", ""); instead of the function Greetings Kaine

              R 1 Reply Last reply
              0
              • K KaineDunno

                Do you mean something like this? Regex.Replace(statementString, @"\[|dbo].|\]", ""); instead of the function Greetings Kaine

                R Offline
                R Offline
                Ryno Burger
                wrote on last edited by
                #10

                I was thinking of Regex but wasn't sure. Thanks for that Kain!

                1 Reply Last reply
                0
                • R Ryno Burger

                  Is there a neater more professional way to re-write the code below? public string StripString(string statementString) { string alteredStatement = string.Empty; string keyword1 = "[dbo]."; string keyword2 = "["; string keyword3 = "]"; string remover = ""; string tmpStr1 = statementString.Replace(keyword1, remover); string tmpStr2 = tmpStr1.Replace(keyword2, remover); alteredStatement = tmpStr2.Replace(keyword3, remover); return alteredStatement; }

                  A Offline
                  A Offline
                  Andrew Rissing
                  wrote on last edited by
                  #11

                  I'm not sure why you're removing the brackets and 'dbo' from SQL. It could open you up to syntax errors, especially since you don't account for the context whatsoever in these sort of find/replace methods. Plus, I remember reading somewhere that stating 'dbo' will improve lookup performance ;) But whatever...just throwing that out there as a word of caution.

                  1 Reply Last reply
                  0
                  • R Ryno Burger

                    Is there a neater more professional way to re-write the code below? public string StripString(string statementString) { string alteredStatement = string.Empty; string keyword1 = "[dbo]."; string keyword2 = "["; string keyword3 = "]"; string remover = ""; string tmpStr1 = statementString.Replace(keyword1, remover); string tmpStr2 = tmpStr1.Replace(keyword2, remover); alteredStatement = tmpStr2.Replace(keyword3, remover); return alteredStatement; }

                    J Offline
                    J Offline
                    Judah Gabriel Himango
                    wrote on last edited by
                    #12

                    This one is more concise and more efficient since it doesn't create unnecessary temporary string instances.

                    public string StripString(string statementString)
                            {
                                string[] keywords = { "[dbo].", "[", "]" };
                                StringBuilder result = new StringBuilder(statementString);
                                foreach (string keyword in keywords) result.Replace(keyword, string.Empty);

                    return result.ToString();
                            }

                    Now, let me ask you a question. It appears you're trying to parse a connection string - is that what you're trying to do? Are you aware there's already a class in the .NET framework that parses a connection string and give you back the interesting pieces of information? That would be OdbcConnection; pass a connection string in the constructor and you'll have a connection object containing all the properties of the connection string.

                    Tech, life, family, faith: Give me a visit. I'm currently blogging about: Feast of Tabernacles (audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                    1 Reply Last reply
                    0
                    • R Ryno Burger

                      Hi guys How can I delete all occurrences of the word 'test' from a string I.e. String Before: This is a test to delete all occurrences of the word test String After: This is a to delete all occurrences of the word Please give me some guidelines here. Thanks R

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #13

                      string s = "this is a test"; s.Replace("test", "");

                      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                      1 Reply Last reply
                      0
                      • R Ryno Burger

                        Hi guys How can I delete all occurrences of the word 'test' from a string I.e. String Before: This is a test to delete all occurrences of the word test String After: This is a to delete all occurrences of the word Please give me some guidelines here. Thanks R

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #14

                        Hi, all replies may lead to damaged sentences such as "Nuclear s damaged the conants inines", and basically ignore the subject line. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                        A 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          Hi, all replies may lead to damaged sentences such as "Nuclear s damaged the conants inines", and basically ignore the subject line. :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                          A Offline
                          A Offline
                          Anthony Mushrow
                          wrote on last edited by
                          #15

                          Actually, i think a couple of the replies inlcuded a space after the word test, so "test ". That would only ruin words ending in test. And besides, if you do it the way i said originally (though of course, i had forgotten about string.replace) you can remove " test " with spaces either side. And then when you re-build the string, add the spaces back in.

                          My current favourite word is: Waffle Cheese is still good though.

                          L 1 Reply Last reply
                          0
                          • A Anthony Mushrow

                            Actually, i think a couple of the replies inlcuded a space after the word test, so "test ". That would only ruin words ending in test. And besides, if you do it the way i said originally (though of course, i had forgotten about string.replace) you can remove " test " with spaces either side. And then when you re-build the string, add the spaces back in.

                            My current favourite word is: Waffle Cheese is still good though.

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #16

                            Words are not necessarily surrounded by spaces, there could be punctuation marks too. This contest holds two "test" words for you to test. :)

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                            A 1 Reply Last reply
                            0
                            • L Luc Pattyn

                              Words are not necessarily surrounded by spaces, there could be punctuation marks too. This contest holds two "test" words for you to test. :)

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                              A Offline
                              A Offline
                              Anthony Mushrow
                              wrote on last edited by
                              #17

                              Yar, stupid punctuation, ruined the english language. :rolleyes: I concede to your brilliance.

                              My current favourite word is: Waffle Cheese is still good though.

                              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