Normalizing strings + make array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gustaf Liljegren

    #1

    Normalizing strings + make array

    I'd like a simple way of extracting everything that's not whitespace on each
    line in a file. For example, if the program encounters this line

    1 2\t\t3 \t\t4

    I want a string array like this

    {"1", "2", "3", "4"}

    I tried working with String.Split() but found that it doesn't automatically
    remove the empty elements in the resulting array. I haven't found any
    normalize method in the API either, to replace the sequences of two or more
    whitespace characters to a single space. Hope there's something I've
    overlooked.

    Thanks,

    Gustaf

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Normalizing strings + make array

    Gustaf,

    You might want to take a look at regular expressions for this. There
    are escape sequences which can eliminate whitespace (I believe \w) and then
    will split apart the string appropriately.

    However, someone else in the group might be better suited to providing
    the actual pattern. You will have to use the Regex class in the
    System.Text.Reg ularExpressions namespace.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Gustaf Liljegren" <gustaf.liljegr en@bredband.net > wrote in message
    news:e15DJfykDH A.2328@TK2MSFTN GP10.phx.gbl...[color=blue]
    > I'd like a simple way of extracting everything that's not whitespace on[/color]
    each[color=blue]
    > line in a file. For example, if the program encounters this line
    >
    > 1 2\t\t3 \t\t4
    >
    > I want a string array like this
    >
    > {"1", "2", "3", "4"}
    >
    > I tried working with String.Split() but found that it doesn't[/color]
    automatically[color=blue]
    > remove the empty elements in the resulting array. I haven't found any
    > normalize method in the API either, to replace the sequences of two or[/color]
    more[color=blue]
    > whitespace characters to a single space. Hope there's something I've
    > overlooked.
    >
    > Thanks,
    >
    > Gustaf
    >[/color]


    Comment

    • Marc Lewandowski

      #3
      Re: Normalizing strings + make array

      Gustaf,

      Check out System.Text.Reg ularExpressions .Regex. You probably want to match
      against "\S*".

      -Marc


      "Gustaf Liljegren" <gustaf.liljegr en@bredband.net > wrote in message
      news:e15DJfykDH A.2328@TK2MSFTN GP10.phx.gbl...[color=blue]
      > I'd like a simple way of extracting everything that's not whitespace on[/color]
      each[color=blue]
      > line in a file. For example, if the program encounters this line
      >
      > 1 2\t\t3 \t\t4
      >
      > I want a string array like this
      >
      > {"1", "2", "3", "4"}
      >
      > I tried working with String.Split() but found that it doesn't[/color]
      automatically[color=blue]
      > remove the empty elements in the resulting array. I haven't found any
      > normalize method in the API either, to replace the sequences of two or[/color]
      more[color=blue]
      > whitespace characters to a single space. Hope there's something I've
      > overlooked.
      >
      > Thanks,
      >
      > Gustaf
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Normalizing strings + make array

        Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c om> wrote:[color=blue]
        > You might want to take a look at regular expressions for this. There
        > are escape sequences which can eliminate whitespace (I believe \w) and then
        > will split apart the string appropriately.[/color]

        \w is "word characters". \s is whitespace. (I say, after trying \w and
        being surprised when it didn't work :)
        [color=blue]
        > However, someone else in the group might be better suited to providing
        > the actual pattern. You will have to use the Regex class in the
        > System.Text.Reg ularExpressions namespace.[/color]

        I believe the pattern wanted here is @"\s+" with the string being
        trimmed to start with.

        Here's a sample program:

        using System;
        using System.Text.Reg ularExpressions ;

        class Test
        {
        static void Main()
        {
        string t = " 1 2\t\t3 \t\t4 ";

        string[] x = Regex.Split (t.Trim(), @"\s+");
        foreach (string s in x)
        {
        Console.WriteLi ne ("'{0}'", s);
        }
        }
        }

        That produces the output:

        '1'
        '2'
        '3'
        '4'

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Marc Lewandowski

          #5
          Re: Normalizing strings + make array

          Jon,

          A more straightforward approach would be to use a matching regex, @"\S+",
          non-whitespace words:

          string thing = " 1 2\t\t3 \t\t4 john\ndoe h.e-r_e's s;o:m'e other words ";

          MatchCollection words = Regex.Matches(t hing, "\\S+");

          foreach (Match m in words)

          Console.WriteLi ne(m.Value);

          -Marc

          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.19f76e 0a93151cbc98989 b@msnews.micros oft.com...[color=blue]
          > Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c om> wrote:[color=green]
          > > You might want to take a look at regular expressions for this.[/color][/color]
          There[color=blue][color=green]
          > > are escape sequences which can eliminate whitespace (I believe \w) and[/color][/color]
          then[color=blue][color=green]
          > > will split apart the string appropriately.[/color]
          >
          > \w is "word characters". \s is whitespace. (I say, after trying \w and
          > being surprised when it didn't work :)
          >[color=green]
          > > However, someone else in the group might be better suited to[/color][/color]
          providing[color=blue][color=green]
          > > the actual pattern. You will have to use the Regex class in the
          > > System.Text.Reg ularExpressions namespace.[/color]
          >
          > I believe the pattern wanted here is @"\s+" with the string being
          > trimmed to start with.
          >
          > Here's a sample program:
          >
          > using System;
          > using System.Text.Reg ularExpressions ;
          >
          > class Test
          > {
          > static void Main()
          > {
          > string t = " 1 2\t\t3 \t\t4 ";
          >
          > string[] x = Regex.Split (t.Trim(), @"\s+");
          > foreach (string s in x)
          > {
          > Console.WriteLi ne ("'{0}'", s);
          > }
          > }
          > }
          >
          > That produces the output:
          >
          > '1'
          > '2'
          > '3'
          > '4'
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          • Gustaf Liljegren

            #6
            Re: Normalizing strings + make array

            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote
            [color=blue]
            > Here's a sample program:[/color]

            Thanks! Very helpful indeed.
            [color=blue]
            > string[] x = Regex.Split (t.Trim(), @"\s+");[/color]

            Yes, this works fine.

            But I just found another obstacle. The syntax in the input files allows
            fields to be quoted if they contains spaces. So this

            1 "2 3"\t\t4 \t\t5

            should be splitted into

            {"1", "2 3", "4", "5"}

            This means whitespace should be removed only if it appears outside double
            quotes (U+0034). And I'm a regex newbie. Maybe this is a question for
            comp.lang.awk, but if someone knows how to contruct this regex, please post.
            :-)

            Thanks,

            Gustaf

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Normalizing strings + make array

              Gustaf Liljegren <gustaf.liljegr en@bredband.net > wrote:[color=blue]
              > But I just found another obstacle. The syntax in the input files allows
              > fields to be quoted if they contains spaces. So this
              >
              > 1 "2 3"\t\t4 \t\t5
              >
              > should be splitted into
              >
              > {"1", "2 3", "4", "5"}
              >
              > This means whitespace should be removed only if it appears outside double
              > quotes (U+0034). And I'm a regex newbie. Maybe this is a question for
              > comp.lang.awk, but if someone knows how to contruct this regex, please post.
              > :-)[/color]

              Ah - at that stage, I would personally ditch regular expressions and
              write the parsing bit yourself. It shouldn't be too hard and the code
              shouldn't be too slow afterwards.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Nicholas Paldino [.NET/C# MVP]

                #8
                Re: Normalizing strings + make array

                Jon,

                I told you that someone else would be better suited in crafting the
                regular expression! =)


                --
                - Nicholas Paldino [.NET/C# MVP]
                - mvp@spam.guard. caspershouse.co m

                "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                news:MPG.19f76e 0a93151cbc98989 b@msnews.micros oft.com...[color=blue]
                > Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c om> wrote:[color=green]
                > > You might want to take a look at regular expressions for this.[/color][/color]
                There[color=blue][color=green]
                > > are escape sequences which can eliminate whitespace (I believe \w) and[/color][/color]
                then[color=blue][color=green]
                > > will split apart the string appropriately.[/color]
                >
                > \w is "word characters". \s is whitespace. (I say, after trying \w and
                > being surprised when it didn't work :)
                >[color=green]
                > > However, someone else in the group might be better suited to[/color][/color]
                providing[color=blue][color=green]
                > > the actual pattern. You will have to use the Regex class in the
                > > System.Text.Reg ularExpressions namespace.[/color]
                >
                > I believe the pattern wanted here is @"\s+" with the string being
                > trimmed to start with.
                >
                > Here's a sample program:
                >
                > using System;
                > using System.Text.Reg ularExpressions ;
                >
                > class Test
                > {
                > static void Main()
                > {
                > string t = " 1 2\t\t3 \t\t4 ";
                >
                > string[] x = Regex.Split (t.Trim(), @"\s+");
                > foreach (string s in x)
                > {
                > Console.WriteLi ne ("'{0}'", s);
                > }
                > }
                > }
                >
                > That produces the output:
                >
                > '1'
                > '2'
                > '3'
                > '4'
                >
                > --
                > Jon Skeet - <skeet@pobox.co m>
                > http://www.pobox.com/~skeet
                > If replying to the group, please do not mail me too[/color]


                Comment

                • Michael Bray

                  #9
                  Re: Normalizing strings + make array

                  Jon Skeet [C# MVP] <skeet@pobox.co m> wrote in
                  news:MPG.19f783 7d5df4925298989 d@msnews.micros oft.com:
                  [color=blue]
                  > Gustaf Liljegren <gustaf.liljegr en@bredband.net > wrote:[color=green]
                  >> This means whitespace should be removed only if it appears outside
                  >> double quotes (U+0034). And I'm a regex newbie. Maybe this is a
                  >> question for comp.lang.awk, but if someone knows how to contruct this
                  >> regex, please post.
                  >> :-)[/color]
                  >
                  > Ah - at that stage, I would personally ditch regular expressions and
                  > write the parsing bit yourself. It shouldn't be too hard and the code
                  > shouldn't be too slow afterwards.[/color]

                  I wrote some code that needed to do this as well - my solution was still to
                  split with the Regex, but then I do some checking to look for double quotes
                  at the start and end of the strings, and when I find a pair I just join
                  them back together. Of course this means you will lose the knowledge of
                  exactly WHAT whitespace ("was it a tab or a space?") was separating them,
                  but in my case it didn't matter... just a thought.

                  -mbray

                  Comment

                  Working...