Results 1 to 2 of 2
  1. #1
    hiranya is offline Member
    Join Date
    Jun 2007
    Location
    Colombo, Sri Lanka
    Posts
    32
    Rep Power
    0

    Default String Manipulation Task

    Hi All,

    I have a String of the following form.

    Java Code:
    "Some Text (Some Integer)"
    Some examples:
    Java Code:
    "Tom Cruise (125)"
    "Mel Gibson (10)"
    How do I extract only the number part within the brackets? A sample code would be appreciated.

    Regards,
    Hiranya

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    First find the index of '(' and ')' signs, other words first and last occurrence of them within the string.

    Java Code:
    String ss = "Tom Cruise (125)";
    
    // First occurrence of '('
    int firstIndex = ss.indexOf('('); // gives a value
    
    // Last occurrence of ')'
    int lastIndex = ss.indexOf(')'); //  or you can use lastIndexOf()
    then find the substring using calculated values

    Java Code:
    String subSt = ss.substring((firstIndex + 1), (lastIndex - 1));
    Hope it helps to you.

Similar Threads

  1. PLZ...PLZ!!!HELP ME TO WRITE THE TASK 2's CODES
    By hhoa87 in forum Advanced Java
    Replies: 3
    Last Post: 03-22-2008, 11:50 PM
  2. java : how to hide application icon from a task bar
    By yogeshagashe in forum Advanced Java
    Replies: 0
    Last Post: 03-12-2008, 01:05 PM
  3. String manipulation example (Title case)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 09:04 AM
  4. Scheduling a task
    By bugger in forum Advanced Java
    Replies: 3
    Last Post: 12-04-2007, 12:32 PM
  5. Task Blocks 0.5
    By johnt in forum Java Software
    Replies: 0
    Last Post: 08-08-2007, 08:43 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
  •