Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting a space in a string 1

Status
Not open for further replies.

jisoo22

Programmer
Apr 30, 2001
277
0
0
US
Hello all,

Is there an effective way to detect a space while parse a string using a for loop? Something like:

Code:
String s = string;
for (int i = 0; i < s.length(); i++){
     if s.charAt(i) == ? ;
     return s.substring(0, i);
}

Thanks,
Jisoo22
 


You could use a StringTokenizer to do what you want I think. Here is a lift from the javadoc fro StringTokenizer...


StringTokenizer st = new StringTokenizer(&quot;this is a test&quot;);
while (st.hasMoreTokens()) {
println(st.nextToken());
}

prints the following output:

this
is
a
test

 
I think u should use a very simple method to detect string as follows:

The input parameters are

s - String in which character is to detected
c - character to be detected. In your case, it is space &quot; &quot;

// Function to remove the specific char.
function removeChar(s,c)
{
var r=&quot;&quot;;
for(i=0;i<s.length;i ++)
{
if (s.charAt(i) != c)
r += s.charAt(i);
}
return r;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top