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. Remove all characters in a string up to and including a specified pattern

Remove all characters in a string up to and including a specified pattern

Scheduled Pinned Locked Moved C#
csharpregexhelp
8 Posts 6 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.
  • M Offline
    M Offline
    MarkB123
    wrote on last edited by
    #1

    Hi, can someone give any advice on the following problem I have in C# 2... I have a string such as "this_is_a test|XXmoretexthere". I need a bit of code that if I specify |xx as the sepertor, will return back to me everything after "|xx" so I have "moretexthere" as a result. The pattern I pass may vary content and length. The current pattern is held in a string variable. Should the pattern occur more than once in the string, I only need to return everything after the first occurrence. Whatever solution I use, it needs to be fast as it will run in a loop possibly many thousands of times. Many thanks for any advice.

    P S O R 4 Replies Last reply
    0
    • M MarkB123

      Hi, can someone give any advice on the following problem I have in C# 2... I have a string such as "this_is_a test|XXmoretexthere". I need a bit of code that if I specify |xx as the sepertor, will return back to me everything after "|xx" so I have "moretexthere" as a result. The pattern I pass may vary content and length. The current pattern is held in a string variable. Should the pattern occur more than once in the string, I only need to return everything after the first occurrence. Whatever solution I use, it needs to be fast as it will run in a loop possibly many thousands of times. Many thanks for any advice.

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

      A Regular Expression, or IndexOf and Substring.

      1 Reply Last reply
      0
      • M MarkB123

        Hi, can someone give any advice on the following problem I have in C# 2... I have a string such as "this_is_a test|XXmoretexthere". I need a bit of code that if I specify |xx as the sepertor, will return back to me everything after "|xx" so I have "moretexthere" as a result. The pattern I pass may vary content and length. The current pattern is held in a string variable. Should the pattern occur more than once in the string, I only need to return everything after the first occurrence. Whatever solution I use, it needs to be fast as it will run in a loop possibly many thousands of times. Many thanks for any advice.

        S Offline
        S Offline
        sanforjackass
        wrote on last edited by
        #3

        it is only an idea you can use find and replace first to change any '|xx' with ';' then use string properties to find the place !!!!!

        OriginalGriffO 1 Reply Last reply
        0
        • S sanforjackass

          it is only an idea you can use find and replace first to change any '|xx' with ';' then use string properties to find the place !!!!!

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          He did say he wanted something fast: Yours involves two searches and two string copies. (Remember strings are immutable, so find and replace will create a new copy of the string, rather than alter the existing one.)

          All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • M MarkB123

            Hi, can someone give any advice on the following problem I have in C# 2... I have a string such as "this_is_a test|XXmoretexthere". I need a bit of code that if I specify |xx as the sepertor, will return back to me everything after "|xx" so I have "moretexthere" as a result. The pattern I pass may vary content and length. The current pattern is held in a string variable. Should the pattern occur more than once in the string, I only need to return everything after the first occurrence. Whatever solution I use, it needs to be fast as it will run in a loop possibly many thousands of times. Many thanks for any advice.

            O Offline
            O Offline
            OkkiePepernoot
            wrote on last edited by
            #5

            string initialText = "this_is_a test|XXmoretexthere";
            string searchString = "|XX";
            int lastIndexFound = initialText.LastIndexOf(searchString);
            if (lastIndexFound > 0)
            {
            MessageBox.Show(initialText.Substring(lastIndexFound + searchString.Length));
            }
            else
            {
            MessageBox.Show("Patternt not found");
            }

            • Not sure if this is fast enough...
            • Not sure if the search is case sensitive otherwise....
            M 1 Reply Last reply
            0
            • O OkkiePepernoot

              string initialText = "this_is_a test|XXmoretexthere";
              string searchString = "|XX";
              int lastIndexFound = initialText.LastIndexOf(searchString);
              if (lastIndexFound > 0)
              {
              MessageBox.Show(initialText.Substring(lastIndexFound + searchString.Length));
              }
              else
              {
              MessageBox.Show("Patternt not found");
              }

              • Not sure if this is fast enough...
              • Not sure if the search is case sensitive otherwise....
              M Offline
              M Offline
              MarkB123
              wrote on last edited by
              #6

              Hi folks, thanks for the replies. Regarding a RegEX, can someone supply an example of the correct syntax for my requirements. I've tried Googling this but I just get confused by it's syntax. Thanks.

              1 Reply Last reply
              0
              • M MarkB123

                Hi, can someone give any advice on the following problem I have in C# 2... I have a string such as "this_is_a test|XXmoretexthere". I need a bit of code that if I specify |xx as the sepertor, will return back to me everything after "|xx" so I have "moretexthere" as a result. The pattern I pass may vary content and length. The current pattern is held in a string variable. Should the pattern occur more than once in the string, I only need to return everything after the first occurrence. Whatever solution I use, it needs to be fast as it will run in a loop possibly many thousands of times. Many thanks for any advice.

                R Offline
                R Offline
                Ramkithepower
                wrote on last edited by
                #7

                Use this string[] str1 =str.Split(new string[] { "|xx" }, 2,StringSplitOptions.None); The second value that is str1[1] will have the value you are looking for. Do let me know if this is not the one you were looking for..

                Jack Sparrow --------------------------------------

                M 1 Reply Last reply
                0
                • R Ramkithepower

                  Use this string[] str1 =str.Split(new string[] { "|xx" }, 2,StringSplitOptions.None); The second value that is str1[1] will have the value you are looking for. Do let me know if this is not the one you were looking for..

                  Jack Sparrow --------------------------------------

                  M Offline
                  M Offline
                  MarkB123
                  wrote on last edited by
                  #8

                  Thanks Jack, that also worked and never occurred to me. Would it be quicker than the LastIndexOf method 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