0

I have a String like this:

http://www.fam.com/FAM#Bruno12/06/2011

How can I cut http://www.fam.com/FAM# and 12/06/2011 in order to get only Bruno.

The format is always:

http://www.fam.com/FAM#NAMEDATE

Is there a simple way to do this? Can you just explain me how?

3
  • 5
    What have you tried? I mean besides asking random strangers on the internet to do it for you. Commented Aug 30, 2012 at 11:32
  • subString method is your friend. Commented Aug 30, 2012 at 11:35
  • I wouldn't use substring. It's not very future-proof and doesn't well express in code that he's after the part section of a URL. Commented Aug 30, 2012 at 11:38

5 Answers 5

1

Simply do this:

myString = original.substring(23, original.length() - 10);
  • 23 is for http://www.fam.com/FAM#
  • original.length() - 10 is for 12/06/2011
Sign up to request clarification or add additional context in comments.

Comments

1

Use :

String str = "http://www.fam.com/FAM#Bruno12/06/2011";
String[] arr = str.split("#|[\\d+/]"); // last index of arr is Bruno

Comments

0

If the string always starts with http://www.fam.com/FAM# then it's simple: that's 23 characters, so take the substring from position 23 (note that indices are zero-based).

String input = "http://www.fam.com/FAM#Bruno12/06/2011";
String result = input.substring(23);

If you want everything after the first # in the string, then search for # and take everything that comes after it:

int index = input.indexOf('#');
String result = input.substring(index + 1);

(error checking omitted for simplicity).

To remove the date, remove the last 10 characters.

See the API documentation of class String for useful methods.

1 Comment

He also wants to remove the date.
0

use regex #(.*?)\\d\\d/ to capture it.

Comments

0

You should use standard URL parsing code, as at Could you share a link to an URL parsing implementation?

I expect the URL parser can cope with the fact that your Ref (i.e. "Bruno12/06/2011") contains slashes.

URL url = new URL(inputString);
String nameDate = url.getRef();

expresses what you want to do in the simplest and clearest form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.