substring until character found..


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: substring until character found..

  1. #1
    Join Date
    Aug 2005
    Posts
    8

    Unhappy substring until character found..

    hello guys

    what i'm trying to do is, getting a substring from the start of String str until space character is found..

    is it somehow feasible? what methods will i need?
    HannaH

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    There are several ways you can do this.

    With substring:
    Code:
    public String firstWord(String str, char delimeter) {
        return str.substring(0, str.indexOf(delimeter));
    }
    With StringBuilder:
    Code:
    public String firstWord(String str, char delimeter) {
        StringBuilder builder = new StringBuilder();
    		
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == delimeter) {
                break;
            }
            builder.append(str.charAt(i));
        }
        return builder.toString();
    }
    With split:
    Code:
    public String firstWord(String str, String delimeter) {
        String[] split = str.split(delimeter);
        return split[0];
    }
    Last edited by destin; 01-15-2006 at 12:19 PM.

  3. #3
    Join Date
    Aug 2005
    Posts
    8
    Thank You Sooo Much

Similar Threads

  1. Replies: 1
    Last Post: 09-17-2008, 04:19 AM
  2. Java Mail Error
    By ddsuresh in forum Java
    Replies: 1
    Last Post: 12-29-2005, 10:58 AM
  3. little help with this substring
    By major in forum Java
    Replies: 1
    Last Post: 12-26-2005, 07:35 PM
  4. Replies: 0
    Last Post: 01-17-2002, 08:06 AM
  5. How to know string's character set
    By Ken in forum VB Classic
    Replies: 1
    Last Post: 01-10-2002, 02:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center