Results 1 to 17 of 17
- 07-07-2011, 12:30 PM #1
Member
- Join Date
- Nov 2010
- Location
- Johannesburg
- Posts
- 23
- Rep Power
- 0
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,
- 07-07-2011, 12:36 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
The lastIndexOf( ... ) and substring( ... ) methods can help you out; read the API documentation for the String class.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-07-2011, 01:18 PM #3
Member
- Join Date
- Nov 2010
- Location
- Johannesburg
- Posts
- 23
- Rep Power
- 0
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
- 07-07-2011, 01:49 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosLast edited by JosAH; 07-07-2011 at 01:53 PM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-08-2011, 05:33 AM #5
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
i tried to code your problem
url entered may beJava 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); } }
Java Code:http://google.com/kkk http://google.com/kkk/lklj https://google.com/kkk
- 07-08-2011, 06:01 AM #6
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).
- 07-08-2011, 08:01 AM #7
Wot no regex? ;)
dbJava 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") + "]"); } } }
- 07-08-2011, 08:09 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 07-08-2011, 08:33 AM #9
- 07-08-2011, 08:47 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-08-2011, 09:16 AM #11
The regex is actually quite fundamental.
Just noticed that OP wants to retain the slash after the domain, so add that to the end of the capturing group.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 timesdbJava Code:String regex = "(^[^/]+(//)?[^/]*/).*";
Last edited by DarrylBurke; 07-08-2011 at 12:49 PM.
- 07-08-2011, 09:53 AM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-08-2011, 05:12 PM #13
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
- 07-08-2011, 05:28 PM #14
You should read up on regular expressions.
- 07-08-2011, 05:30 PM #15
Not many people do. They are very hard to debug. They are a "black-box" type of program that either works or not.i am still not able understand about regex
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.
- 07-08-2011, 05:47 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-11-2011, 11:56 AM #17
Member
- Join Date
- Nov 2010
- Location
- Johannesburg
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
Sorting a String array based on a substring
By jonytek in forum New To JavaReplies: 1Last Post: 06-07-2011, 06:21 AM -
Search Substring in String Help Please
By Kestrel01 in forum New To JavaReplies: 3Last Post: 10-26-2010, 06:48 PM -
How to extract substring from a string based on the condition ??
By j_kathiresan in forum Advanced JavaReplies: 5Last Post: 06-22-2010, 09:34 AM -
Reverse a string not using the substring method
By kathyla18 in forum New To JavaReplies: 17Last Post: 04-08-2009, 04:08 AM -
String substring function
By ravian in forum New To JavaReplies: 6Last Post: 01-02-2008, 07:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
.gif)

Bookmarks