IndexOf for whitespace  
Author Message
Roedy Green





PostPosted: 2008-1-29 3:38:00 Top

java-programmer, IndexOf for whitespace What if you wanted the indexOf either a space or a \n whichever came
first. Have you a slick way to code that?

Pedestrian possibilities include:

1. pair of indexOfs , take minimum ignoring <0s.

2. Write a loop that uses charAt

3. write a regex

Is there a slick way to handle it?
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
 
Matt Humphrey





PostPosted: 2008-1-29 4:28:00 Top

java-programmer >> IndexOf for whitespace
"Roedy Green" <email***@***.com> wrote in message
news:email***@***.com...
> What if you wanted the indexOf either a space or a \n whichever came
> first. Have you a slick way to code that?
>
> Pedestrian possibilities include:
>
> 1. pair of indexOfs , take minimum ignoring <0s.
>
> 2. Write a loop that uses charAt
>
> 3. write a regex
>
> Is there a slick way to handle it?

int p = s.replace('\n', ' ').indexOf (' ');

Matt Humphrey http://www.iviz.com/


 
Roedy Green





PostPosted: 2008-1-29 6:18:00 Top

java-programmer >> IndexOf for whitespace On Mon, 28 Jan 2008 19:38:10 GMT, Roedy Green
<email***@***.com> wrote, quoted or indirectly quoted
someone who said :

>What if you wanted the indexOf either a space or a \n whichever came
>first. Have you a slick way to code that?
>
>Pedestrian possibilities include:
>
>1. pair of indexOfs , take minimum ignoring <0s.
>
>2. Write a loop that uses charAt
>
>3. write a regex
>
>Is there a slick way to handle it?

I handled it by adding a method to the StringTools class.


/**
* find the first instance of whitespace (space, \n, \r, \t in a
string.
*
* @param s string to scan
* @param startOffset where in string to start looking
*
* @return -1 if not found, offset relative to start of string
where found, not relative to startOffset.
*/
public static int indexOfWhiteSpace( String s, int startOffset )
{
final int length = s.length();
for ( int i = startOffset; i < length; i++ )
{
switch ( s.charAt( i ) )
{
case ' ':
case '\n':
case '\r':
case '\t':
return i;
default:
// keep looking
}
}// end for
return -1;
}

--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
 
 
Lew





PostPosted: 2008-1-29 8:19:00 Top

java-programmer >> IndexOf for whitespace Roedy Green wrote:
> On Mon, 28 Jan 2008 19:38:10 GMT, Roedy Green
> <email***@***.com> wrote, quoted or indirectly quoted
> someone who said :
>
>> What if you wanted the indexOf either a space or a \n whichever came
>> first. Have you a slick way to code that?
>>
>> Pedestrian possibilities include:
>>
>> 1. pair of indexOfs , take minimum ignoring <0s.
>>
>> 2. Write a loop that uses charAt
>>
>> 3. write a regex

java.util.regex.Matcher has an end() method that would tell you where the
regex "[ ]" or "\s" or "[\n ]" matches.

--
Lew
 
 
Roedy Green





PostPosted: 2008-1-29 10:05:00 Top

java-programmer >> IndexOf for whitespace On Mon, 28 Jan 2008 19:19:16 -0500, Lew <email***@***.com> wrote,
quoted or indirectly quoted someone who said :

>java.util.regex.Matcher has an end() method that would tell you where the
>regex "[ ]" or "\s" or "[\n ]" matches.

You can't generalise to other sets of characters with my method on the
fly. You could compose a regex expression on the fly.
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com