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. Question regarding C#

Question regarding C#

Scheduled Pinned Locked Moved C#
csharptutorialquestion
16 Posts 6 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.
  • M Member 11532095

    How to split a String on the basis of a specific word... which means a string: My Name Is Ali i want to split the string when ever Is comes.

    A Offline
    A Offline
    Abdulnazark
    wrote on last edited by
    #6

    try this string s="test1 ali test2 ali"; string[] parts = s.Replace("ali", "/").Split('/');

    P 1 Reply Last reply
    0
    • A Abdulnazark

      try this string s="test1 ali test2 ali"; string[] parts = s.Replace("ali", "/").Split('/');

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #7

      And what happens when s contains "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here"?

      A 1 Reply Last reply
      0
      • P Pete OHanlon

        And what happens when s contains "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here"?

        A Offline
        A Offline
        Abdulnazark
        wrote on last edited by
        #8

        static void Main(string[] args) { String s = "aaa ali jskdfhskjdfhk ali sjkhfkjsfhkjsdh ali"; var regex = new Regex("ali", RegexOptions.IgnoreCase); var s1 = regex.Replace(s, "/"); string[] parts = s1.Split('/'); for (int i = 0; i < parts.Length; i++) Console.WriteLine(parts[i]); Console.ReadLine(); }

        P 1 Reply Last reply
        0
        • A Abdulnazark

          static void Main(string[] args) { String s = "aaa ali jskdfhskjdfhk ali sjkhfkjsfhkjsdh ali"; var regex = new Regex("ali", RegexOptions.IgnoreCase); var s1 = regex.Replace(s, "/"); string[] parts = s1.Split('/'); for (int i = 0; i < parts.Length; i++) Console.WriteLine(parts[i]); Console.ReadLine(); }

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #9

          Nope, that's still wrong. Seriously, try it out with the sentence I gave you. There's only one occurrence of the word Ali in that sentence, but "ali" appears as a substring in other words. So, you should only get two phrases - your version returns 4 and you are corrupting the string so characters are being removed.

          A 2 Replies Last reply
          0
          • P Pete OHanlon

            Nope, that's still wrong. Seriously, try it out with the sentence I gave you. There's only one occurrence of the word Ali in that sentence, but "ali" appears as a substring in other words. So, you should only get two phrases - your version returns 4 and you are corrupting the string so characters are being removed.

            A Offline
            A Offline
            Abdulnazark
            wrote on last edited by
            #10

            i tried with your string 'My Name Is Ali' also and it return 'My Name Is'

            P 1 Reply Last reply
            0
            • P Pete OHanlon

              Nope, that's still wrong. Seriously, try it out with the sentence I gave you. There's only one occurrence of the word Ali in that sentence, but "ali" appears as a substring in other words. So, you should only get two phrases - your version returns 4 and you are corrupting the string so characters are being removed.

              A Offline
              A Offline
              Abdulnazark
              wrote on last edited by
              #11

              http://stackoverflow.com/questions/6025560/how-to-ignore-case-in-string-replace

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Because the syntax you have to use to split on string(s) rather than character(s) is so damn clumsy: :laugh:

                        string\[\] parts = input.Split(new string\[\] { " Is " }, StringSplitOptions.None);
                

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                R Offline
                R Offline
                Rob Philpott
                wrote on last edited by
                #12

                Fair point... There is a rather obvious overload missing.

                Regards, Rob Philpott.

                1 Reply Last reply
                0
                • A Abdulnazark

                  i tried with your string 'My Name Is Ali' also and it return 'My Name Is'

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #13

                  You seem to be confusing me with the original poster. My string was "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here". Try that one.

                  A 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    You seem to be confusing me with the original poster. My string was "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here". Try that one.

                    A Offline
                    A Offline
                    Abdulnazark
                    wrote on last edited by
                    #14

                    Now i got you, please try bellow

                    static void Main(string[] args)
                    {
                    String s = "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here";
                    string pattern = @"\bali\b";
                    string replace = "/";
                    var s1 = Regex.Replace(s, pattern, replace, RegexOptions.IgnoreCase);
                    string[] parts = s1.Split('/');
                    for (int i = 0; i < parts.Length; i++)
                    Console.WriteLine(parts[i]+ i.ToString());
                    Console.ReadLine();

                        }
                    
                    Richard DeemingR 1 Reply Last reply
                    0
                    • A Abdulnazark

                      Now i got you, please try bellow

                      static void Main(string[] args)
                      {
                      String s = "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here";
                      string pattern = @"\bali\b";
                      string replace = "/";
                      var s1 = Regex.Replace(s, pattern, replace, RegexOptions.IgnoreCase);
                      string[] parts = s1.Split('/');
                      for (int i = 0; i < parts.Length; i++)
                      Console.WriteLine(parts[i]+ i.ToString());
                      Console.ReadLine();

                          }
                      
                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #15

                      Using the Regex.Split method[^] (as OG suggested[^]) is a better solution:

                      string input = "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here";
                      string pattern = @"\bali\b";

                      string[] parts = Regex.Split(input, pattern, RegexOptions.IgnoreCase);


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      A 1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        Using the Regex.Split method[^] (as OG suggested[^]) is a better solution:

                        string input = "the bestiality displayed by the main character, Ali, suggests that there is a causality at stake here";
                        string pattern = @"\bali\b";

                        string[] parts = Regex.Split(input, pattern, RegexOptions.IgnoreCase);


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        A Offline
                        A Offline
                        Abdulnazark
                        wrote on last edited by
                        #16

                        thanks..

                        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