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