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. String Manipulation

String Manipulation

Scheduled Pinned Locked Moved C#
csharpcomtoolshelpquestion
12 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.
  • P Offline
    P Offline
    Programm3r
    wrote on last edited by
    #1

    Hi all, I have the following string:

    drwxr-xr-x 1 ftp ftp 0 Nov 09 07:04 Windows
    drwxr-xr-x 1 ftp ftp 0 Oct 29 2007 Nvidia
    drwxr-xr-x 1 ftp ftp 0 Jan 01 2003 Program Files

    I would like to extract only the folder name i.e. "Windows" or "Program Files", but my string manipulation isn't so good in C# can anyone please help me? Many Thanks Regards,


    The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

    G 1 Reply Last reply
    0
    • P Programm3r

      Hi all, I have the following string:

      drwxr-xr-x 1 ftp ftp 0 Nov 09 07:04 Windows
      drwxr-xr-x 1 ftp ftp 0 Oct 29 2007 Nvidia
      drwxr-xr-x 1 ftp ftp 0 Jan 01 2003 Program Files

      I would like to extract only the folder name i.e. "Windows" or "Program Files", but my string manipulation isn't so good in C# can anyone please help me? Many Thanks Regards,


      The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      The string looks broken, doesn't all the entries have both a year and a time? If they do, the number of characters before the folder name should be constant, and you can just split the string on Environment.NewLine and use .Substring(41) to get each folder name.

      Experience is the sum of all the mistakes you have done.

      P 1 Reply Last reply
      0
      • G Guffa

        The string looks broken, doesn't all the entries have both a year and a time? If they do, the number of characters before the folder name should be constant, and you can just split the string on Environment.NewLine and use .Substring(41) to get each folder name.

        Experience is the sum of all the mistakes you have done.

        P Offline
        P Offline
        Programm3r
        wrote on last edited by
        #3

        Hi Guffa, Thanks for the response. I don't think the string is broken, as I stated in my previous post, that's how the FTP server (FileZilla) returns the directory list, but I'm on the verge of figuring it out :) Thanks Regards,


        The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

        G 1 Reply Last reply
        0
        • P Programm3r

          Hi Guffa, Thanks for the response. I don't think the string is broken, as I stated in my previous post, that's how the FTP server (FileZilla) returns the directory list, but I'm on the verge of figuring it out :) Thanks Regards,


          The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Ok, is that the only variations there are, or are the items that are listed with both year and time, and items that are listed without both?

          Experience is the sum of all the mistakes you have done.

          P M 2 Replies Last reply
          0
          • G Guffa

            Ok, is that the only variations there are, or are the items that are listed with both year and time, and items that are listed without both?

            Experience is the sum of all the mistakes you have done.

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

            Guffa wrote:

            is that the only variations there are, or are the items that are listed with both year and time, and items that are listed without both?

            Yes that is the only variants there are.


            The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

            1 Reply Last reply
            0
            • G Guffa

              Ok, is that the only variations there are, or are the items that are listed with both year and time, and items that are listed without both?

              Experience is the sum of all the mistakes you have done.

              M Offline
              M Offline
              mabo42
              wrote on last edited by
              #6

              Guffa, this is the way the most ?nixe show their directory entries. For older entries they show date without time, for younger date without year, but with time. But in all cases you can't rely on fixed width column structure. Regards

              P P 2 Replies Last reply
              0
              • M mabo42

                Guffa, this is the way the most ?nixe show their directory entries. For older entries they show date without time, for younger date without year, but with time. But in all cases you can't rely on fixed width column structure. Regards

                P Offline
                P Offline
                Programm3r
                wrote on last edited by
                #7

                mabo42 wrote:

                But in all cases you can't rely on fixed width column structure.

                So what can one rely on ...


                The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

                M 3 Replies Last reply
                0
                • P Programm3r

                  mabo42 wrote:

                  But in all cases you can't rely on fixed width column structure.

                  So what can one rely on ...


                  The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

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

                  The columns you showed are always exist, but the width can differ in width (perhaps not in your case, username is always ftp, directory size is always 0...). To be on the safe side, use RegEx. Regards

                  1 Reply Last reply
                  0
                  • P Programm3r

                    mabo42 wrote:

                    But in all cases you can't rely on fixed width column structure.

                    So what can one rely on ...


                    The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

                    M Offline
                    M Offline
                    mabo42
                    wrote on last edited by
                    #9

                    To make it more clear, this is a sample listing:

                    drwxr-x--- 2 ftp ftp 8192 Nov 19 12:14 .
                    drwxr-xr-x 11 ftp ftp 8192 Nov 19 12:35 ..
                    -rw-r----- 1 someone someone 8639750144 Nov 19 12:14 this_is_a_long_file_name.dat
                    -rw-r----- 1 ftp ftp 6260006912 Nov 19 13:13 shortname.dat

                    The filename (or directory name) is the 9. group (delimited by whitespace) until line end. Regards

                    P 1 Reply Last reply
                    0
                    • P Programm3r

                      mabo42 wrote:

                      But in all cases you can't rely on fixed width column structure.

                      So what can one rely on ...


                      The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

                      M Offline
                      M Offline
                      mabo42
                      wrote on last edited by
                      #10

                      You could try this function, should work:

                      using System.Text.RegularExpressions;

                      public string EntryName(string line)
                      {
                      Regex regex = new Regex(@"[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+(?<ENTRY>.*)");
                      Match match = regex.Match(line);
                      return match.Groups["ENTRY"].Value.Trim();
                      }

                      Regards

                      1 Reply Last reply
                      0
                      • M mabo42

                        To make it more clear, this is a sample listing:

                        drwxr-x--- 2 ftp ftp 8192 Nov 19 12:14 .
                        drwxr-xr-x 11 ftp ftp 8192 Nov 19 12:35 ..
                        -rw-r----- 1 someone someone 8639750144 Nov 19 12:14 this_is_a_long_file_name.dat
                        -rw-r----- 1 ftp ftp 6260006912 Nov 19 13:13 shortname.dat

                        The filename (or directory name) is the 9. group (delimited by whitespace) until line end. Regards

                        P Offline
                        P Offline
                        Programm3r
                        wrote on last edited by
                        #11

                        Thank you mabo42 :) Regards


                        The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

                        1 Reply Last reply
                        0
                        • M mabo42

                          Guffa, this is the way the most ?nixe show their directory entries. For older entries they show date without time, for younger date without year, but with time. But in all cases you can't rely on fixed width column structure. Regards

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

                          Yeah, one of the worst things about Unix.

                          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