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. More elegant way then .Split(']');

More elegant way then .Split(']');

Scheduled Pinned Locked Moved C#
5 Posts 5 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.
  • R Offline
    R Offline
    Rick van Woudenberg
    wrote on last edited by
    #1

    Dear All, I have ListView full of things, but basically all the items have the same format : TY223 [16] TW121 [223] ... ... If I want to substract the value 16 and TY223 from TY223 [16] , I go about it like this :

                string\[\] a = itm.Value.ToString().Split('\[');
                string\[\] b = a\[1\].ToString().Split('\]');
    
                string first = a\[1\].ToString().Trim();
                string second = b\[0\].ToString().Trim();
    

    This works fine, however I'm only after the value 16. Is there a more elegant way of extracting this value from the item, instead of using

    .Split()

    Kind regards,

    A M M P 4 Replies Last reply
    0
    • R Rick van Woudenberg

      Dear All, I have ListView full of things, but basically all the items have the same format : TY223 [16] TW121 [223] ... ... If I want to substract the value 16 and TY223 from TY223 [16] , I go about it like this :

                  string\[\] a = itm.Value.ToString().Split('\[');
                  string\[\] b = a\[1\].ToString().Split('\]');
      
                  string first = a\[1\].ToString().Trim();
                  string second = b\[0\].ToString().Trim();
      

      This works fine, however I'm only after the value 16. Is there a more elegant way of extracting this value from the item, instead of using

      .Split()

      Kind regards,

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #2

      You might be able to do something like (I'd normally use something like this for reading from a file, but it seems to work here too):

      string ReadWord(string buffer, char delimiter, int startPos)
      {
      StringBuilder sb = new StringBuilder();
      for(int i=startPos; i<buffer.Length; i++)
      {
      if(buffer[i] == delimiter)
      break;

          sb.append(buffer\[i\]);
      }
      
      return sb.toString();
      

      }

      void main()
      {
      string firstPart = ReadWord(fullLine, ' ', 0);
      int startPos = fullLine.IndexOf('[');
      string secondPart = ReadWord(fullLine, ']', startPos);
      }

      Thats just from memory so it may not compile, but you get the idea.

      My current favourite word is: Delicious!

      -SK Genius

      Game Programming articles start -here[^]-

      modified on Monday, May 18, 2009 7:42 AM

      1 Reply Last reply
      0
      • R Rick van Woudenberg

        Dear All, I have ListView full of things, but basically all the items have the same format : TY223 [16] TW121 [223] ... ... If I want to substract the value 16 and TY223 from TY223 [16] , I go about it like this :

                    string\[\] a = itm.Value.ToString().Split('\[');
                    string\[\] b = a\[1\].ToString().Split('\]');
        
                    string first = a\[1\].ToString().Trim();
                    string second = b\[0\].ToString().Trim();
        

        This works fine, however I'm only after the value 16. Is there a more elegant way of extracting this value from the item, instead of using

        .Split()

        Kind regards,

        M Offline
        M Offline
        monstale
        wrote on last edited by
        #3

        Hi, I like using regex:

        System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"[ [\]]");
        label1.Text = "";
        foreach (String s in r.Split(textBox2.Text))
        {
        label1.Text += s + "\n";
        }

        You might use index 0 and 2 and you don't need .Trim() again. ;-) Have fun

        1 Reply Last reply
        0
        • R Rick van Woudenberg

          Dear All, I have ListView full of things, but basically all the items have the same format : TY223 [16] TW121 [223] ... ... If I want to substract the value 16 and TY223 from TY223 [16] , I go about it like this :

                      string\[\] a = itm.Value.ToString().Split('\[');
                      string\[\] b = a\[1\].ToString().Split('\]');
          
                      string first = a\[1\].ToString().Trim();
                      string second = b\[0\].ToString().Trim();
          

          This works fine, however I'm only after the value 16. Is there a more elegant way of extracting this value from the item, instead of using

          .Split()

          Kind regards,

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          how about, in one line...

          string resultString = inputString.Substring(inputString.IndexOf('[') + 1, inputString.IndexOf(']') - inputString.IndexOf('[') - 1);

          Life goes very fast. Tomorrow, today is already yesterday.

          1 Reply Last reply
          0
          • R Rick van Woudenberg

            Dear All, I have ListView full of things, but basically all the items have the same format : TY223 [16] TW121 [223] ... ... If I want to substract the value 16 and TY223 from TY223 [16] , I go about it like this :

                        string\[\] a = itm.Value.ToString().Split('\[');
                        string\[\] b = a\[1\].ToString().Split('\]');
            
                        string first = a\[1\].ToString().Trim();
                        string second = b\[0\].ToString().Trim();
            

            This works fine, however I'm only after the value 16. Is there a more elegant way of extracting this value from the item, instead of using

            .Split()

            Kind regards,

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

            I think you mean "extract" the value. Regular Expression. I'd use something like: "\[(?'Value'.*)\]"

            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