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 operation

String operation

Scheduled Pinned Locked Moved C#
question
12 Posts 8 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.
  • L Lost User

    Are they delimited by spaces? Are there always the same number of elements in your string? If so, you could split[^] it on the space.

    I are Troll :suss:

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

    This line of string is not delimited by spaces.Yes,this particular line always contains same number of elements. Is there any efficient method other than split?

    L H 2 Replies Last reply
    0
    • S SRKSHOME

      Hi, I have following string. And I have to extract "931.3392ms" from the string. This line of string is not delimited by any character. How do I extract it? 8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA_LOAD_TYPE.Deep) 931.3392ms (1.7/11.4 KB)

      R Offline
      R Offline
      Rajesh Anuhya
      wrote on last edited by
      #4

      Try This

      string str = "8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA\_LOAD\_TYPE.Deep) 931.3392ms (1.7/11.4 KB)";
      

      MessageBox.Show(
      (((str.Substring(str.IndexOf(")") + 1)).
      Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).
      Substring(0, ((str.Substring(str.IndexOf(")") + 1)).
      Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).IndexOf("(")))
      );

      Rajesh B --> A Poor Workman Blames His Tools <--

      S M L 3 Replies Last reply
      0
      • R Rajesh Anuhya

        Try This

        string str = "8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA\_LOAD\_TYPE.Deep) 931.3392ms (1.7/11.4 KB)";
        

        MessageBox.Show(
        (((str.Substring(str.IndexOf(")") + 1)).
        Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).
        Substring(0, ((str.Substring(str.IndexOf(")") + 1)).
        Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).IndexOf("(")))
        );

        Rajesh B --> A Poor Workman Blames His Tools <--

        S Offline
        S Offline
        SRKSHOME
        wrote on last edited by
        #5

        Thanks...This works efficiently.

        1 Reply Last reply
        0
        • S SRKSHOME

          This line of string is not delimited by spaces.Yes,this particular line always contains same number of elements. Is there any efficient method other than split?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          SRKSHOME wrote:

          This line of string is not delimited by spaces.

          You already mentioned that, just checking since there were spaces in the example-string.

          SRKSHOME wrote:

          Yes,this particular line always contains same number of elements.

          Split on some other character? Like the "("?

          SRKSHOME wrote:

          Yes,this particular line always contains same number of elements.

          But the elements do not always have the same length?

          SRKSHOME wrote:

          Is there any efficient method other than split?

          Yup, using regular expressions[^].

          I are Troll :suss:

          1 Reply Last reply
          0
          • R Rajesh Anuhya

            Try This

            string str = "8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA\_LOAD\_TYPE.Deep) 931.3392ms (1.7/11.4 KB)";
            

            MessageBox.Show(
            (((str.Substring(str.IndexOf(")") + 1)).
            Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).
            Substring(0, ((str.Substring(str.IndexOf(")") + 1)).
            Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).IndexOf("(")))
            );

            Rajesh B --> A Poor Workman Blames His Tools <--

            M Offline
            M Offline
            Md Marufuzzaman
            wrote on last edited by
            #7

            Gr8... very smart. :)

            Thanks Md. Marufuzzaman


            I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

            P 1 Reply Last reply
            0
            • R Rajesh Anuhya

              Try This

              string str = "8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA\_LOAD\_TYPE.Deep) 931.3392ms (1.7/11.4 KB)";
              

              MessageBox.Show(
              (((str.Substring(str.IndexOf(")") + 1)).
              Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).
              Substring(0, ((str.Substring(str.IndexOf(")") + 1)).
              Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).IndexOf("(")))
              );

              Rajesh B --> A Poor Workman Blames His Tools <--

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

              I can't disagree more, that is horrible code. It is unreadable, it has 7 calls to IndexOf where you only need 3, and 7 to SubString where you also need just 3.

              string a=str.Substring(str.IndexOf(")") + 1);   // skip everything before first ")"
              string b=a.Substring(a.IndexOf(")") + 1);       // skip everything before second ")"
              string c=b.Substring(0, b.IndexOf("("));        // drop everything after next "("
              

              The Regex alternatives are pretty simple too, here is one; the result excludes the surrounding spaces:

              string d=Regex.Match(str, @"\\) (\[0-9\\.\]\*ms) \\(").Groups\[1\].Value;
              

              :)

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


              Prolific encyclopedia fixture proof-reader browser patron addict?
              We all depend on the beast below.


              1 Reply Last reply
              0
              • S SRKSHOME

                Hi, I have following string. And I have to extract "931.3392ms" from the string. This line of string is not delimited by any character. How do I extract it? 8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA_LOAD_TYPE.Deep) 931.3392ms (1.7/11.4 KB)

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

                Use a Regular Expression, check out Expresso[^] or my own RegexTester[^].

                1 Reply Last reply
                0
                • M Md Marufuzzaman

                  Gr8... very smart. :)

                  Thanks Md. Marufuzzaman


                  I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

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

                  +5 for sarcasm.

                  1 Reply Last reply
                  0
                  • S SRKSHOME

                    This line of string is not delimited by spaces.Yes,this particular line always contains same number of elements. Is there any efficient method other than split?

                    H Offline
                    H Offline
                    hossein narimani rad 0
                    wrote on last edited by
                    #11

                    if the string is always fix size you can simply try the Substring method of the String class.

                    1 Reply Last reply
                    0
                    • S SRKSHOME

                      Hi, I have following string. And I have to extract "931.3392ms" from the string. This line of string is not delimited by any character. How do I extract it? 8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA_LOAD_TYPE.Deep) 931.3392ms (1.7/11.4 KB)

                      R Offline
                      R Offline
                      Ravi Bhavnani
                      wrote on last edited by
                      #12

                      You may want to consider using my StringParser[^] class.

                      string mSec = string.Empty;
                      StringParser sp = new StringParser (s);
                      if (sp.skipToEndOf (") ") && sp.skipToEndOf (") ")) {
                      sp.extractTo ("(", out mSec);
                      }

                      /ravi

                      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                      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