DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  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 11:19 AM.

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

Similar Threads

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

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links