0

I got stuck in this regular expression as I am having a string as:

   String str = "/abcde/samplename.xyz"

I want to replace this samplename.xyz from a new string, so how can I apply the regular expression in this ?

3
  • would you like to only remove samplename.xyz and result will be /abcde/? Commented Jan 9, 2014 at 6:41
  • yes with /abcde/ I will append another string value after replacing samplename.xyz Commented Jan 9, 2014 at 6:42
  • please see my answer and other two, all the answer now correct. Commented Jan 9, 2014 at 6:55

5 Answers 5

3

Try this out.....

    String str = "/abcde/samplename.xyz";

    String req=str.substring(str.lastIndexOf("/") + 1);

now you get the value of: req=samplename.xyz and you can replace with which ever string value you want

    String rep=str.replace(req, "");
Sign up to request clarification or add additional context in comments.

Comments

2
String startString=str.substring(0, str.lastIndexOf('/') + 1)

1 Comment

Yes you right man, I have also done the same thing. +1 for you.
1
 String str = "/abcde/samplename.xyz";
    String str1 = str.substring(0, str.lastIndexOf('/') + 1);
    Toast.makeText(getApplicationContext(), str1, Toast.LENGTH_LONG).show();
    Log.i("strArr[1]=", "" + str1);

check this.i hope its useful to you.

2 Comments

no, it will split from the the first backslash..I want it to split from the last backslash
but it will only give abcde , but he needs /abcde/
1

Try this::

String str = "/abcde/samplename.xyz";
        String result=str.substring(0, str.lastIndexOf('/') + 1);
        Log.d("Hello","Result="+result);

Comments

1
 String[] result = str.split("/");

 String LastItem = result[str.length -1];

 LastItem.replace(newString);

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.