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 Operations

String Operations

Scheduled Pinned Locked Moved C#
data-structures
20 Posts 7 Posters 1 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.
  • W wasifmuneer

    i Have passed an string array into string.split function,the target string splits successfully,but i need to get string array information on which string has been split.

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

    Hi, it is not very clear. if you want to split a string s1 using a collection of split characters (say all chars in string s2) and are interested in which split characters actually occur in s1, then this strange snippet offers exactly that; I leave it to the reader to discover how it works!

    string[] sa=s1.Split(string.Join("", s1.Split(s2.ToCharArray())).ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

    :)

    Luc Pattyn [Forum Guidelines] [My Articles]


    Love, happiness and fewer bugs for 2009!


    D W D J 4 Replies Last reply
    0
    • L Luc Pattyn

      Hi, it is not very clear. if you want to split a string s1 using a collection of split characters (say all chars in string s2) and are interested in which split characters actually occur in s1, then this strange snippet offers exactly that; I leave it to the reader to discover how it works!

      string[] sa=s1.Split(string.Join("", s1.Split(s2.ToCharArray())).ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

      :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Love, happiness and fewer bugs for 2009!


      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #6

      Very cool Luc :cool:

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

      1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, it is not very clear. if you want to split a string s1 using a collection of split characters (say all chars in string s2) and are interested in which split characters actually occur in s1, then this strange snippet offers exactly that; I leave it to the reader to discover how it works!

        string[] sa=s1.Split(string.Join("", s1.Split(s2.ToCharArray())).ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

        :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Love, happiness and fewer bugs for 2009!


        W Offline
        W Offline
        wasifmuneer
        wrote on last edited by
        #7

        Its Looking Really cool,,thanks for Help but one thing more i have have string array collection, not char array collection,so how can i do this with string array collection..

        L 1 Reply Last reply
        0
        • W wasifmuneer

          Its Looking Really cool,,thanks for Help but one thing more i have have string array collection, not char array collection,so how can i do this with string array collection..

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

          if you want the result to be a char array, you have to turn my result into a char array; I have already shown how that is done. It is just more of the same stuff. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Love, happiness and fewer bugs for 2009!


          1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, it is not very clear. if you want to split a string s1 using a collection of split characters (say all chars in string s2) and are interested in which split characters actually occur in s1, then this strange snippet offers exactly that; I leave it to the reader to discover how it works!

            string[] sa=s1.Split(string.Join("", s1.Split(s2.ToCharArray())).ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Love, happiness and fewer bugs for 2009!


            D Offline
            D Offline
            Dewald
            wrote on last edited by
            #9

            Luc Pattyn wrote:

            this strange snippet offers exactly that

            neat

            1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, it is not very clear. if you want to split a string s1 using a collection of split characters (say all chars in string s2) and are interested in which split characters actually occur in s1, then this strange snippet offers exactly that; I leave it to the reader to discover how it works!

              string[] sa=s1.Split(string.Join("", s1.Split(s2.ToCharArray())).ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

              :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Love, happiness and fewer bugs for 2009!


              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #10

              I'm glad I don't have to maintain that piece of code. Curious to see what a LINQ-based solution would read like.

              Tech, life, family, faith: Give me a visit. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

              L 1 Reply Last reply
              0
              • J Judah Gabriel Himango

                I'm glad I don't have to maintain that piece of code. Curious to see what a LINQ-based solution would read like.

                Tech, life, family, faith: Give me a visit. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

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

                The good thing is it doesn't need any maintenance :laugh: ; it could be implemented as an extension method to the string class, I would suggest it gets named RemoveNot(s1,s2): remove all chars from s1 that are not in s2. No idea what LINQ can do here. And also no idea what regex could do (I was surprised no one ever mentioned it so far). I am pretty sure both of them would look even worse, and have lower performance. A lambda expression might come in handy, haven't really considered it (am still working in .NET 2.0) :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Love, happiness and fewer bugs for 2009!


                G 1 Reply Last reply
                0
                • L Luc Pattyn

                  The good thing is it doesn't need any maintenance :laugh: ; it could be implemented as an extension method to the string class, I would suggest it gets named RemoveNot(s1,s2): remove all chars from s1 that are not in s2. No idea what LINQ can do here. And also no idea what regex could do (I was surprised no one ever mentioned it so far). I am pretty sure both of them would look even worse, and have lower performance. A lambda expression might come in handy, haven't really considered it (am still working in .NET 2.0) :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Love, happiness and fewer bugs for 2009!


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

                  Luc Pattyn wrote:

                  And also no idea what regex could do (I was surprised no one ever mentioned it so far).

                  Getting there, just have to read the replies so far first... :)

                  Despite everything, the person most likely to be fooling you next is yourself.

                  L 1 Reply Last reply
                  0
                  • W wasifmuneer

                    i Have passed an string array into string.split function,the target string splits successfully,but i need to get string array information on which string has been split.

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

                    This code will split the string, and give you the result as two arrays; one array with the regular split result, and one array with the splitters that was used. The usedSplitters array is one item shorter than the words array. If no splitters were found, the words array contain the original string, and the usedSplitters array is empty.

                    string source = "asdf,asdf;;;asdf,asdf::asdf..asdf";
                    string[] splitters = { "..", ",", ";;;", "::" };

                    string pattern = "(" + string.Join("|", splitters.Select(s => Regex.Escape(s)).ToArray()) + ")";
                    MatchCollection matches = Regex.Matches(source, pattern);
                    string[] words = new string[matches.Count + 1];
                    string[] usedSplitters = new string[matches.Count];
                    int pos = 0;
                    for (int i = 0; i < matches.Count; i++) {
                    words[i] = source.Substring(pos, matches[i].Index - pos);
                    usedSplitters[i] = matches[i].Value;
                    pos = matches[i].Index + matches[i].Length;
                    }
                    words[matches.Count] = source.Substring(pos);

                    Despite everything, the person most likely to be fooling you next is yourself.

                    L 1 Reply Last reply
                    0
                    • G Guffa

                      Luc Pattyn wrote:

                      And also no idea what regex could do (I was surprised no one ever mentioned it so far).

                      Getting there, just have to read the replies so far first... :)

                      Despite everything, the person most likely to be fooling you next is yourself.

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

                      I'll hold the line

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      Love, happiness and fewer bugs for 2009!


                      1 Reply Last reply
                      0
                      • G Guffa

                        This code will split the string, and give you the result as two arrays; one array with the regular split result, and one array with the splitters that was used. The usedSplitters array is one item shorter than the words array. If no splitters were found, the words array contain the original string, and the usedSplitters array is empty.

                        string source = "asdf,asdf;;;asdf,asdf::asdf..asdf";
                        string[] splitters = { "..", ",", ";;;", "::" };

                        string pattern = "(" + string.Join("|", splitters.Select(s => Regex.Escape(s)).ToArray()) + ")";
                        MatchCollection matches = Regex.Matches(source, pattern);
                        string[] words = new string[matches.Count + 1];
                        string[] usedSplitters = new string[matches.Count];
                        int pos = 0;
                        for (int i = 0; i < matches.Count; i++) {
                        words[i] = source.Substring(pos, matches[i].Index - pos);
                        usedSplitters[i] = matches[i].Value;
                        pos = matches[i].Index + matches[i].Length;
                        }
                        words[matches.Count] = source.Substring(pos);

                        Despite everything, the person most likely to be fooling you next is yourself.

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

                        I like the concept, my Visual Studio targetting 3.5 does not know the Select method though. Anyway, it is not one of those obscure regex applications... :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        Love, happiness and fewer bugs for 2009!


                        G 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          I like the concept, my Visual Studio targetting 3.5 does not know the Select method though. Anyway, it is not one of those obscure regex applications... :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          Love, happiness and fewer bugs for 2009!


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

                          I think that you need using System.Xml.Linq; to get the Select extension. Here's a way to create the pattern without it:

                          StringBuilder builder = new StringBuilder().Append('(');
                          bool first = true;
                          foreach (string s in splitters) {
                          if (first) {
                          first = false;
                          } else {
                          builder.Append('|');
                          }
                          builder.Append(Regex.Escape(s));
                          }
                          string pattern = builder.Append(')').ToString();

                          Despite everything, the person most likely to be fooling you next is yourself.

                          L 1 Reply Last reply
                          0
                          • G Guffa

                            I think that you need using System.Xml.Linq; to get the Select extension. Here's a way to create the pattern without it:

                            StringBuilder builder = new StringBuilder().Append('(');
                            bool first = true;
                            foreach (string s in splitters) {
                            if (first) {
                            first = false;
                            } else {
                            builder.Append('|');
                            }
                            builder.Append(Regex.Escape(s));
                            }
                            string pattern = builder.Append(')').ToString();

                            Despite everything, the person most likely to be fooling you next is yourself.

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

                            the original works like a charm once I added using System.Linq; Without Select, I would write

                            StringBuilder builder = new StringBuilder().Append('(');
                            string sep="";
                            foreach (string s in splitters) {
                            builder.Append(sep).Append(Regex.Escape(s));
                            sep="|";
                            }
                            string pattern = builder.Append(')').ToString();

                            avoiding the extra state variable and if-else test. Thanks. :)

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            Love, happiness and fewer bugs for 2009!


                            G 1 Reply Last reply
                            0
                            • L Luc Pattyn

                              the original works like a charm once I added using System.Linq; Without Select, I would write

                              StringBuilder builder = new StringBuilder().Append('(');
                              string sep="";
                              foreach (string s in splitters) {
                              builder.Append(sep).Append(Regex.Escape(s));
                              sep="|";
                              }
                              string pattern = builder.Append(')').ToString();

                              avoiding the extra state variable and if-else test. Thanks. :)

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              Love, happiness and fewer bugs for 2009!


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

                              Is it better? You are just replacing one state variable with another, making the value of the variable double as both state and value. :)

                              Despite everything, the person most likely to be fooling you next is yourself.

                              L 1 Reply Last reply
                              0
                              • G Guffa

                                Is it better? You are just replacing one state variable with another, making the value of the variable double as both state and value. :)

                                Despite everything, the person most likely to be fooling you next is yourself.

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

                                I trust this is subjective, however I see lots of small reasons, yes: I find it shorter, more readable, less error prone, and possibly better for instruction scheduling. Also for testing and proofing correctness avoiding if-tests generally is beneficial. :)

                                Luc Pattyn [Forum Guidelines] [My Articles]


                                Love, happiness and fewer bugs for 2009!


                                1 Reply Last reply
                                0
                                • W wasifmuneer

                                  i Have passed an string array into string.split function,the target string splits successfully,but i need to get string array information on which string has been split.

                                  realJSOPR Offline
                                  realJSOPR Offline
                                  realJSOP
                                  wrote on last edited by
                                  #20

                                  If you mean that you want to get the string that was originally split from the array, you will either have to write a method to do that, or you can use my handy-dandy string parsing class, available here[^] on CodeProject.

                                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                  -----
                                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                                  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