
Signature
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
> What if you wanted the indexOf either a space or a \n whichever came
> first. Have you a slick way to code that?
[quoted text clipped - 8 lines]
>
> Is there a slick way to handle it?
int p = s.replace('\n', ' ').indexOf (' ');
Matt Humphrey http://www.iviz.com/
On Mon, 28 Jan 2008 19:38:10 GMT, Roedy Green
<see_website@mindprod.com.invalid> 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?
[quoted text clipped - 8 lines]
>
>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;
}

Signature
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
Lew - 29 Jan 2008 00:19 GMT
> On Mon, 28 Jan 2008 19:38:10 GMT, Roedy Green
> <see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
[quoted text clipped - 10 lines]
>>
>> 3. write a regex
java.util.regex.Matcher has an end() method that would tell you where the
regex "[ ]" or "\s" or "[\n ]" matches.

Signature
Lew
Roedy Green - 29 Jan 2008 02:05 GMT
>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.

Signature
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
> What if you wanted the indexOf either a space or a \n whichever came
> first. Have you a slick way to code that?
[quoted text clipped - 8 lines]
>
> Is there a slick way to handle it?
import java.util.*;
import java.util.regex.*;
public class test3 {
public static void main(String[] args) {
String str = "nowisthetimeforall good";
System.out.println(indexOfRegex(str,"\\s+"));
}
public static int indexOfRegex(String str, String regex) {
int retcod = -1;
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if (m.find())
retcod = m.start();
return retcod;
}
}

Signature
Knute Johnson
email s/nospam/knute/
------->>>>>>http://www.NewsDem