Results 1 to 8 of 8
  1. #1
    vidya is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default Extracting the text between two strings

    Hi friends... I have a doubt in extracting the text between two strings. for example:

    Begin THE EXCEPTION WAS HANDLED end

    Now, How i need to find if the line has 'Begin' at the start and 'end' at the end of the string. after that, i need to extract the text 'THE EXCEPTION WAS HANDLED' alone,and store it somewhere.
    How can i do that...?

    It would be helpful if someone helps me with this query...
    Last edited by vidya; 02-11-2010 at 06:02 AM.

  2. #2
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    take whole as one string
    and use substring() to take the required string from the whole string.

  3. #3
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    public class StringDemo {
    public static void main(String[] args){
    String string = "Begin THE EXCEPTION WAS HANDLED end";
    System.out.println("String : " + string);
    String substring = string.substring(0, 5);
    System.out.println(substring);
    String substring1 = string.substring(31, 35);
    System.out.println(substring1);
    String substring2 = string.substring(6, 31);
    System.out.println(substring2);


    }

    output wud be:

    run:
    String : Begin THE EXCEPTION WAS HANDLED end
    Begin
    end
    THE EXCEPTION WAS HANDLED
    BUILD SUCCESSFUL (total time: 0 seconds)

  4. #4
    vidya is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    Thanks for ur quick reply friend. i got the same idea of sub string previously(using 'regionMatches' method). but what u used here was the offset value to extract the sub string. but if we consider a huge paragraph, where we dont know that offset value, its not possible to use this method to retrieve the string right?

    so what can be done for this?

  5. #5
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    well another option you can consider is using split method from the string class.

  6. #6
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    There are lots of methods in String class.
    For String manipulation, i suggest you to look at method available in String class.

  7. #7
    vidya is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Smile

    @All

    Thanks for ur suggestions. I wil try string class and get back if i find any difficulty.

  8. #8
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    regex is another alternative.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

Similar Threads

  1. searchin for more Strings in a Text
    By gazale_m in forum New To Java
    Replies: 8
    Last Post: 12-09-2008, 08:51 AM
  2. extracting text from jpeg
    By Nicholas Jordan in forum Advanced Java
    Replies: 0
    Last Post: 10-05-2008, 11:40 PM
  3. Replies: 5
    Last Post: 07-18-2008, 04:30 PM
  4. Extracting JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:17 AM
  5. text Strings to produce letters
    By dc2acgsr99 in forum New To Java
    Replies: 7
    Last Post: 01-29-2008, 08:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •