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. Position of Characters in a String

Position of Characters in a String

Scheduled Pinned Locked Moved C#
questionc++help
7 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.
  • G Offline
    G Offline
    gonad
    wrote on last edited by
    #1

    Hi How can i find the position of each of the spaces in this string? String duh = "Its a beautiful day in the neighborhood"; int c = 0; char[] s = duh.ToCharArray(); for(int i = 0; i < s.Length; i++) { if(s[i].ToString() == " ") { c++; Console.WriteLine(c); } } This code finds the 6 spaces in the string, but i'm having a hard time getting the IndexOf location of each of these spaces. Any help would be appreciated.

    J F B 3 Replies Last reply
    0
    • G gonad

      Hi How can i find the position of each of the spaces in this string? String duh = "Its a beautiful day in the neighborhood"; int c = 0; char[] s = duh.ToCharArray(); for(int i = 0; i < s.Length; i++) { if(s[i].ToString() == " ") { c++; Console.WriteLine(c); } } This code finds the 6 spaces in the string, but i'm having a hard time getting the IndexOf location of each of these spaces. Any help would be appreciated.

      J Offline
      J Offline
      Julian Bucknall MSFT
      wrote on last edited by
      #2

      How about string s = "It's a beautiful day in the neighborhood"; for (int index = 0; index < length(s); index++) { if (s[index] == ' ') { Console.WriteLine(index); } } Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

      D G 2 Replies Last reply
      0
      • G gonad

        Hi How can i find the position of each of the spaces in this string? String duh = "Its a beautiful day in the neighborhood"; int c = 0; char[] s = duh.ToCharArray(); for(int i = 0; i < s.Length; i++) { if(s[i].ToString() == " ") { c++; Console.WriteLine(c); } } This code finds the 6 spaces in the string, but i'm having a hard time getting the IndexOf location of each of these spaces. Any help would be appreciated.

        F Offline
        F Offline
        Frank Olorin Rizzi
        wrote on last edited by
        #3

        My cheap $0.02: Option #1: the same code you have up there, but put the value of i (when s[i] is equal to " ") in a collection (Hashtable, ArrayList, what have you). Option #2: use IndexOf(" ") and cut the starting part of the string every time... string s1 = "Its a beautiful day to be coding"; string s2 = s1; //To save the original s1 if needed Hashtable ht = new Hashtable(); //Use whatever you need instead of an hashtable while(s2.Length>0) { int k = s2.IndexOf(" "); if(k<0) { break; //No more spaces found }//IF ht.Add(ht.Count, k); //Save in ht the index where the next space is found s2 = s2.Substr(k+1); //Cut away everything up to the space }//WEND //Disclaimer: I have not compiled nor tested this code Maybe it could be better to use a StringBuilder :-) HTH, Olorin

        1 Reply Last reply
        0
        • J Julian Bucknall MSFT

          How about string s = "It's a beautiful day in the neighborhood"; for (int index = 0; index < length(s); index++) { if (s[index] == ' ') { Console.WriteLine(index); } } Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

          D Offline
          D Offline
          David Stone
          wrote on last edited by
          #4

          Welcome to CP Julian! Always good to have more Microsofties around. :)


          Hawaian shirts and shorts work too in Summer. People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage... -Anna-Jayne Metcalfe on Paintballing

          1 Reply Last reply
          0
          • J Julian Bucknall MSFT

            How about string s = "It's a beautiful day in the neighborhood"; for (int index = 0; index < length(s); index++) { if (s[index] == ' ') { Console.WriteLine(index); } } Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

            G Offline
            G Offline
            gonad
            wrote on last edited by
            #5

            Thanks for your help on this. it worked as you said, except it's actually index < s.Length; also, i was using " " instead of ' ' as you did and i was getting a compilation error. i didn't think it mattered, but i was wrong. :) Thanks again.

            J 1 Reply Last reply
            0
            • G gonad

              Thanks for your help on this. it worked as you said, except it's actually index < s.Length; also, i was using " " instead of ' ' as you did and i was getting a compilation error. i didn't think it mattered, but i was wrong. :) Thanks again.

              J Offline
              J Offline
              Julian Bucknall MSFT
              wrote on last edited by
              #6

              :) I should've added my usual clause: (Code was written on the fly, probably won't compile.) The difference between " " and ' ' is that the first is a string of length one, and the second is a single character. So you can't compare a character such as someString[i] to " " (they're two different types) if (someString[i] == " ") { // won't compile // blah } ...but you can to ' ' (since they're both of type char) if (someString[i] == ' ') { // will compile and do what you think // blah } Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

              1 Reply Last reply
              0
              • G gonad

                Hi How can i find the position of each of the spaces in this string? String duh = "Its a beautiful day in the neighborhood"; int c = 0; char[] s = duh.ToCharArray(); for(int i = 0; i < s.Length; i++) { if(s[i].ToString() == " ") { c++; Console.WriteLine(c); } } This code finds the 6 spaces in the string, but i'm having a hard time getting the IndexOf location of each of these spaces. Any help would be appreciated.

                B Offline
                B Offline
                Bo Hunter
                wrote on last edited by
                #7

                this works fairly well, I subtracted 1 from the for loop because it prints out 33 for what would be the next space. This is the output. 4 6 16 20 23 26 private void button1_Click(object sender, System.EventArgs e) { string str = "Its a beautiful day to be coding"; string[] strs = str.Split( new char[]{' '} ); int j = 0; int k = 0; for ( int i = 0; i < strs.Length - 1; i++ ) { k = strs[i].Length + 1; Debug.WriteLine( k + j ); j += strs[i].Length + 1; } } Bo Hunter

                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