Results 1 to 17 of 17
  1. #1
    Unnel is offline Member
    Join Date
    Nov 2010
    Location
    Johannesburg
    Posts
    23
    Rep Power
    0

    Default How do I remove a substring from a string?

    Hi,

    I've developed an application that reads URLs from a file. I would like to know how I can chop off a part of a string I'm not interested in. Below is an example of what I mean.

    http://www.aghdncbyd.com/ghjjsyk

    I would like to remove the 'ghjjsyk' part after the last foreslash so that I can store the rest.

    Thank you,

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    The lastIndexOf( ... ) and substring( ... ) methods can help you out; read the API documentation for the String class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Unnel is offline Member
    Join Date
    Nov 2010
    Location
    Johannesburg
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    The lastIndexOf( ... ) and substring( ... ) methods can help you out; read the API documentation for the String class.

    kind regards,

    Jos
    Thanks Jos,

    But what about long URLS such as http://www.aghdtfsj.com/hsgdtt/ghdtuii/?

    How do I go about only retrieving http://www.aghdtfs.com/ and chopping off the rest? I'm looking for a generic way of doing this because my URLs are differing from one another.

    Thank you

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Unnel View Post
    Thanks Jos,

    But what about long URLS such as http://www.aghdtfsj.com/hsgdtt/ghdtuii/?

    How do I go about only retrieving http://www.aghdtfs.com/ and chopping off the rest? I'm looking for a generic way of doing this because my URLs are differing from one another.

    Thank you
    Start your search for a / from the front of the String; you want to stop ,and chop off, the String starting at the third / from the start of the String. Read the API documentation for the two argument indefOf( ... ) method in the String class; or else, use the URL class; it can do such things such as chopping file, host and protocol parts.

    kind regards,

    Jos
    Last edited by JosAH; 07-07-2011 at 01:53 PM.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    i tried to code your problem
    Java Code:
    import java.lang.String;
    class Str
    {
    	public static void main(String ar[])
    	{
    		String s1="/";
    		int i;
    		i=ar[0].indexOf(s1,8);
    		String ns;
    		ns=ar[0].substring(0,i);
    		System.out.println("String ="+ns);
    	}
    }
    url entered may be
    Java Code:
    http://google.com/kkk  
    http://google.com/kkk/lklj
    https://google.com/kkk

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    One thing you can do to make the code a bit more robust in case the http:// bit is not present is to do an indexOf "//". If it is greater than 0 then indexOf "/" starting from that position (2 parameter method). Else indexOf "/" from the start of the URL (1 parameter method).

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Wot no regex? ;)
    Java Code:
    public class DomainOnly {
    
      public static void main(String[] args) {
        String[] inputs = {
          "http://www.aghdncbyd.com/ghjjsyk",
          "http://www.aghdtfsj.com/hsgdtt/ghdtuii/?",
          "http://google.com/kkk",
          "http://google.com/kkk/lklj",
          "https://google.com/kkk"
        };
        String regex = "(^[^/]+(//)?[^/]*).*";
        for (String input : inputs) {
          System.out.println("[" + input.replaceAll(regex, "$1") + "]");
        }
      }
    }
    db

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DarrylBurke View Post
    Wot no regex? ;)
    Java Code:
        String regex = "(^[^/]+(//)?[^/]*).*";
    How come I didn't think of that gibberish; /*&(^^*?//?\// to you too ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Don't you ever do that again; I didn't have my espresso yet nor did I smoke my first tobacco and you're showing me that mess? I'm a very vulnerable little puppy at this hour of the day you know, *yuck!*

    kind regards,

    Jos (<--- goes to take a shower)
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    The regex is actually quite fundamental.
    Java Code:
    (         start capturing group 1
    ^         start of input
    [^/]+     not-slash, one or more times
    (//)?     double-slash, 0 or 1 time
    [^/]*     not-slash, 0 or more times
    )         end capturing group 1
    .*        any character, 0 or more times
    
    "$1"      the content of capturing group 1: 
          start of input
          + not-slash, one or more times
          + double-slash, 0 or 1 time
          + not-slash, 0 or more times
    Just noticed that OP wants to retain the slash after the domain, so add that to the end of the capturing group.
    Java Code:
    String regex = "(^[^/]+(//)?[^/]*/).*";
    db
    Last edited by DarrylBurke; 07-08-2011 at 12:49 PM.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DarrylBurke View Post
    The regex is actually quite fundamental.
    Yes I know; type 2 (context free) grammars are also fundamental and so much more powerful; I really don't understand the popularity of regular expressions. There's nothing a RE can do that a type 2 grammar parser can't do ... and those REs are ugly to.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post
    The regex is actually quite fundamental.
    Java Code:
    (         start capturing group 1
    ^         start of input
    [^/]+     not-slash, one or more times
    (//)?     double-slash, 0 or 1 time
    [^/]*     not-slash, 0 or more times
    )         end capturing group 1
    .*        any character, 0 or more times
    
    "$1"      the content of capturing group 1: 
          start of input
          + not-slash, one or more times
          + double-slash, 0 or 1 time
          + not-slash, 0 or more times
    Just noticed that OP wants to retain the slash after the domain, so add that to the end of the capturing group.
    Java Code:
    String regex = "(^[^/]+(//)?[^/]*/).*";
    db
    i am still not able understand about regex

  14. #14
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    You should read up on regular expressions.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    i am still not able understand about regex
    Not many people do. They are very hard to debug. They are a "black-box" type of program that either works or not.

    Is there a way to debug them? A way to trace the logic flow as they are scanned and rescanned etc???

    They are great when they work and a head ache when they don't.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    Not many people do. They are very hard to debug. They are a "black-box" type of program that either works or not.

    Is there a way to debug them? A way to trace the logic flow as they are scanned and rescanned etc???

    They are great when they work and a head ache when they don't.
    Regular expressions are a PITA but they don't need to be rescanned all the time. Most (all?) RE engines build a NFA (Non-deterministic Finit Automaton) out of the RE which is often turned into a DFA (Deterministic Finit Automaton) which can be interpreted in linear time (O(n)) given an input String. The size of the DFA can be O(2^n) w.r.t. to original RE though. I don't think Sun's/Oracle's implementation can be easily debuged ... but I agree: they can be (and often are) a head ache.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    Unnel is offline Member
    Join Date
    Nov 2010
    Location
    Johannesburg
    Posts
    23
    Rep Power
    0

    Default

    Hi guys,

    Thanks a lot for your much appreciated and valuable input. I managed to get my piece of code to work thanks to y'all.

    Have a good one.

    Cheers

Similar Threads

  1. Sorting a String array based on a substring
    By jonytek in forum New To Java
    Replies: 1
    Last Post: 06-07-2011, 06:21 AM
  2. Search Substring in String Help Please
    By Kestrel01 in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 06:48 PM
  3. Replies: 5
    Last Post: 06-22-2010, 09:34 AM
  4. Reverse a string not using the substring method
    By kathyla18 in forum New To Java
    Replies: 17
    Last Post: 04-08-2009, 04:08 AM
  5. String substring function
    By ravian in forum New To Java
    Replies: 6
    Last Post: 01-02-2008, 07:35 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
  •