• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Trim leading or trailing characters from a string?  RSS feed

 
Victor Ade
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I return Www.indigo.com from http://www.indigo.com/emirate ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class java.lang.String has lots of useful methods to work with strings. Look at the API documentation to see which methods you could use for this.

But, before that, think the problem through. Can you explain yourself, in plain English, how you would solve this problem? If you have a string "http://www.indigo.com/emirate", then what would be a good way to find the "www.indigo.com" part? (Hint: Maybe you would want to look for the "/" characters in the string).
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please also provide a bit more information, how the topic subject is related with your current problem defined in the post?

Victor Ade / subject wrote:How to Trim leading or trailing characters from a string?

Victor Ade / post wrote:How can I return Www.indigo.com from http://www.indigo.com/emirate ?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a URL and you want to extract the host name, then using a java.net.URL object and calling a suitable method would do that for you. But perhaps that's not exactly your requirement...
 
Victor Ade
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Class java.lang.String has lots of useful methods to work with strings. Look at the API documentation to see which methods you could use for this.

But, before that, think the problem through. Can you explain yourself, in plain English, how you would solve this problem? If you have a string "http://www.indigo.com/emirate", then what would be a good way to find the "www.indigo.com" part? (Hint: Maybe you would want to look for the "/" characters in the string).



Thanks for the hint jasper. Since i left this here I've done some more research and found that I needed to use the substring( lastindexof()) method. I'm using it right now. Thanks for the hint again!
 
Victor Ade
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Class java.lang.String has lots of useful methods to work with strings. Look at the API documentation to see which methods you could use for this.

But, before that, think the problem through. Can you explain yourself, in plain English, how you would solve this problem? If you have a string "http://www.indigo.com/emirate", then what would be a good way to find the "www.indigo.com" part? (Hint: Maybe you would want to look for the "/" characters in the string).

Actually this is what i ended up using:

String str = "http://www.anywebsite.com/bsvsvahshas/sjsbga";

                            String dd = strl.substring(str.indexOf("/") + 2);
                        
                             String gg = dd.split("/")[0];
RESULT?? www.anywebsite.com;
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably you could think a bit further and improve your algorithm.

Think how you'd solve the problem if the address would be provided in the next form: www.anywebsite.com/bsvsvahshas/sjsbga
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!