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. split strings.

split strings.

Scheduled Pinned Locked Moved C#
data-structuresperformance
5 Posts 3 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.
  • A Offline
    A Offline
    AndieDu
    wrote on last edited by
    #1

    Hi All, I have this string: "1x #234 Greenock Creek - Alice's - Shiraz - 2001 - 750mL = $578.05 1x #242 Greenock Creek - Cornerstone - Grenache - 2001 - 750mL = $297.55 2x #363 Maxwell - Ellen Street - Shiraz - 2002 - 750mL = $309.10 2x #369 Maxwell - Meracus 53 - Shiraz Grenache - 2002 - 750mL = $226.60" Now i want to split this string like this: 1x #234 Greenock Creek - Alice's - Shiraz - 2001 - 750mL = $578.05 1x #242 Greenock Creek - Cornerstone - Grenache - 2001 - 750mL = $297.55 2x #363 Maxwell - Ellen Street - Shiraz - 2002 - 750mL = $309.10 2x #369 Maxwell - Meracus 53 - Shiraz Grenache - 2002 - 750mL = $226.60 I was tring to load this string into an array of char, but the performance will be affected since i have a pretty large string input. Are there any other ways to split this string. Thanks very much.

    P A P 3 Replies Last reply
    0
    • A AndieDu

      Hi All, I have this string: "1x #234 Greenock Creek - Alice's - Shiraz - 2001 - 750mL = $578.05 1x #242 Greenock Creek - Cornerstone - Grenache - 2001 - 750mL = $297.55 2x #363 Maxwell - Ellen Street - Shiraz - 2002 - 750mL = $309.10 2x #369 Maxwell - Meracus 53 - Shiraz Grenache - 2002 - 750mL = $226.60" Now i want to split this string like this: 1x #234 Greenock Creek - Alice's - Shiraz - 2001 - 750mL = $578.05 1x #242 Greenock Creek - Cornerstone - Grenache - 2001 - 750mL = $297.55 2x #363 Maxwell - Ellen Street - Shiraz - 2002 - 750mL = $309.10 2x #369 Maxwell - Meracus 53 - Shiraz Grenache - 2002 - 750mL = $226.60 I was tring to load this string into an array of char, but the performance will be affected since i have a pretty large string input. Are there any other ways to split this string. Thanks very much.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You can do this with a fairly naive regex. I'm no regex expert, but I quickly knocked this one together which should satisfy your requirements here:

      Regex regex = new Regex(
      @"\d+x\s\#\w*[^$]+\$\d+\.\d+",
      RegexOptions.IgnoreCase
      | RegexOptions.Multiline
      | RegexOptions.ExplicitCapture
      | RegexOptions.IgnorePatternWhitespace
      | RegexOptions.Compiled
      );

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      A 1 Reply Last reply
      0
      • P Pete OHanlon

        You can do this with a fairly naive regex. I'm no regex expert, but I quickly knocked this one together which should satisfy your requirements here:

        Regex regex = new Regex(
        @"\d+x\s\#\w*[^$]+\$\d+\.\d+",
        RegexOptions.IgnoreCase
        | RegexOptions.Multiline
        | RegexOptions.ExplicitCapture
        | RegexOptions.IgnorePatternWhitespace
        | RegexOptions.Compiled
        );

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        A Offline
        A Offline
        AndieDu
        wrote on last edited by
        #3

        Thanks for your posting, but you regular expression is not working. and this is supposed to be: @"([0-9][x]\s#[0-9]+\s[A-Z,a-z,\s,',0-9]+\s-\s[A-Z,a-z,\s,',0-9]+\s-\s[A-Z,a-z,\s,',0-9]+\s-\s[0-9]+\s-\s[0-9]+mL\s=\s[$][0-9,.]+)", but it is verbose, and i need to refine it.

        1 Reply Last reply
        0
        • A AndieDu

          Hi All, I have this string: "1x #234 Greenock Creek - Alice's - Shiraz - 2001 - 750mL = $578.05 1x #242 Greenock Creek - Cornerstone - Grenache - 2001 - 750mL = $297.55 2x #363 Maxwell - Ellen Street - Shiraz - 2002 - 750mL = $309.10 2x #369 Maxwell - Meracus 53 - Shiraz Grenache - 2002 - 750mL = $226.60" Now i want to split this string like this: 1x #234 Greenock Creek - Alice's - Shiraz - 2001 - 750mL = $578.05 1x #242 Greenock Creek - Cornerstone - Grenache - 2001 - 750mL = $297.55 2x #363 Maxwell - Ellen Street - Shiraz - 2002 - 750mL = $309.10 2x #369 Maxwell - Meracus 53 - Shiraz Grenache - 2002 - 750mL = $226.60 I was tring to load this string into an array of char, but the performance will be affected since i have a pretty large string input. Are there any other ways to split this string. Thanks very much.

          A Offline
          A Offline
          AndieDu
          wrote on last edited by
          #4

          #)"; string[] substrings = Regex.Split(input, pattern); for (int i = 0; i < substrings.Length; i++) { if (substrings[i] != "") { int j = i + 1; Console.WriteLine(substrings[i] + substrings[j]); i++; } } Console.ReadLine();

          1 Reply Last reply
          0
          • A AndieDu

            Hi All, I have this string: "1x #234 Greenock Creek - Alice's - Shiraz - 2001 - 750mL = $578.05 1x #242 Greenock Creek - Cornerstone - Grenache - 2001 - 750mL = $297.55 2x #363 Maxwell - Ellen Street - Shiraz - 2002 - 750mL = $309.10 2x #369 Maxwell - Meracus 53 - Shiraz Grenache - 2002 - 750mL = $226.60" Now i want to split this string like this: 1x #234 Greenock Creek - Alice's - Shiraz - 2001 - 750mL = $578.05 1x #242 Greenock Creek - Cornerstone - Grenache - 2001 - 750mL = $297.55 2x #363 Maxwell - Ellen Street - Shiraz - 2002 - 750mL = $309.10 2x #369 Maxwell - Meracus 53 - Shiraz Grenache - 2002 - 750mL = $226.60 I was tring to load this string into an array of char, but the performance will be affected since i have a pretty large string input. Are there any other ways to split this string. Thanks very much.

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

            Here's a RegEx: (?'Quantity'\d+)x #(?'Number'\d+) (?'Winery'[^-]+)- (?'Brand'[^-]+)- (?'Type'[^-]+)- (?'Year'\d+) - (?'Size'[^=]+)= (?'Price'[0-9$\.]+) Some of the values may need trimming.

            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