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. Regex.Split help

Regex.Split help

Scheduled Pinned Locked Moved C#
c++regexhelpquestion
6 Posts 4 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
    Aljaz111
    wrote on last edited by
    #1

    I am counting words in string, that i get from text file. Using this code i get always 1 more word in counting. What could be wrong?

    int stevec = 0;
    string s="avc sde s a s"
    string[] words = Regex.Split(s, @"[\S]+");
    foreach (string word in words)
    {
    stevec++;
    }

    After deb stevec has value 6. But there are only 5 words.

    K L A 3 Replies Last reply
    0
    • A Aljaz111

      I am counting words in string, that i get from text file. Using this code i get always 1 more word in counting. What could be wrong?

      int stevec = 0;
      string s="avc sde s a s"
      string[] words = Regex.Split(s, @"[\S]+");
      foreach (string word in words)
      {
      stevec++;
      }

      After deb stevec has value 6. But there are only 5 words.

      K Offline
      K Offline
      Kristian Sixhoj
      wrote on last edited by
      #2

      I tested your regex.. Making the S lowercase did the trick.

      Regex.Split(s, @"[\s]+");

      :bob: Kristian Sixhoej "You can always become better." - Tiger Woods

      1 Reply Last reply
      0
      • A Aljaz111

        I am counting words in string, that i get from text file. Using this code i get always 1 more word in counting. What could be wrong?

        int stevec = 0;
        string s="avc sde s a s"
        string[] words = Regex.Split(s, @"[\S]+");
        foreach (string word in words)
        {
        stevec++;
        }

        After deb stevec has value 6. But there are only 5 words.

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

        gee. Did you consider looking what is inside the words array? to fix a faulty program, you need to open your eyes and look around. Nobody is going to do that for you. If you get all the facts, and still can't figure it out, then post a real question, clear and well documented. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


        A 1 Reply Last reply
        0
        • L Luc Pattyn

          gee. Did you consider looking what is inside the words array? to fix a faulty program, you need to open your eyes and look around. Nobody is going to do that for you. If you get all the facts, and still can't figure it out, then post a real question, clear and well documented. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


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

          Sorry.. while i was debuggin i couldn't see words with breakpoints, even in console they weren't visible. And with greater S using only Regex.Match the counting was correct, thats why i asked here. Anyway, thanks for help.

          L 1 Reply Last reply
          0
          • A Aljaz111

            Sorry.. while i was debuggin i couldn't see words with breakpoints, even in console they weren't visible. And with greater S using only Regex.Match the counting was correct, thats why i asked here. Anyway, thanks for help.

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

            there are many debugging tools, the simplest one is Console.WriteLine; it works always, never had a problem with it. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


            1 Reply Last reply
            0
            • A Aljaz111

              I am counting words in string, that i get from text file. Using this code i get always 1 more word in counting. What could be wrong?

              int stevec = 0;
              string s="avc sde s a s"
              string[] words = Regex.Split(s, @"[\S]+");
              foreach (string word in words)
              {
              stevec++;
              }

              After deb stevec has value 6. But there are only 5 words.

              A Offline
              A Offline
              April Fans
              wrote on last edited by
              #6

              @"[\S]+" should be replaced by @"[\s]+". "s" should be the lowercase.

              April Comm100 - Leading Live Chat Software Provider

              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