For Programmers: Free Programming Magazines  


Home > Archive > Java Help > January 2008 > IndexOf for whitespace









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author IndexOf for whitespace
Roedy Green

2008-01-28, 7:44 pm

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

2008-01-28, 7:44 pm


"Roedy Green" <see_website@mindprod.com.invalid> wrote in message
news:hebsp3pe3ntf706vtp8pbmr7big5ng9lni@
4ax.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

2008-01-28, 7:44 pm

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?
>
>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

2008-01-28, 7:44 pm

Roedy Green wrote:[color=darkred]
> On Mon, 28 Jan 2008 19:38:10 GMT, Roedy Green
> <see_website@mindprod.com.invalid> 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.

--
Lew
Roedy Green

2008-01-28, 10:37 pm

On Mon, 28 Jan 2008 19:19:16 -0500, Lew <lew@lewscanon.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
Knute Johnson

2008-01-28, 10:37 pm

Roedy Green wrote:
> 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?


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;
}
}

--

Knute Johnson
email s/nospam/knute/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDem
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com