Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2008

Tip: Looking for answers? Try searching our database.

IndexOf for whitespace

Thread view: 
Roedy Green - 28 Jan 2008 19:38 GMT
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?
Signature

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

Matt Humphrey - 28 Jan 2008 20:27 GMT
> 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/
Roedy Green - 28 Jan 2008 22:17 GMT
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

Knute Johnson - 29 Jan 2008 04:10 GMT
> 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.