Alpha
Alpha

Reputation: 14026

How to get a specific part of string with specific condition?

I've following kind of string:

http://www.example.org/ab/app/ram/ri/000000000000004

I just want last part of the string i.e. 000000000000004 which changes every time. How can I get it using Java?

Upvotes: 0

Views: 115

Answers (5)

user2010539
user2010539

Reputation:

string yourUrl = "Sample String"

string[] res1= yourUrl.Split('/'); string res2= res1[res1.Length - 1];

Upvotes: 0

ismail baig
ismail baig

Reputation: 891

ofcoure i have put the code in C#, should be slight change with syntax in java.i have posted it just to let the questioner know the direction.

string yourUrl = "http://www.example.org/ab/app/ram/ri/000000000000004";
string[] result = yourUrl.Split('/');
string finalRes = result[result.Length - 1];

Upvotes: 1

Jens
Jens

Reputation: 69440

You can use pattern matching to do that:

    String s ="http://www.example.org/ab/app/ram/ri/000000000000004";

   Pattern p = Pattern.compile("/(\\d+)");
   Matcher m= p.matcher(s);
   if (m.find()){
       System.out.println(m.group(1));
   }

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

you can use Pattern and Matcher classes and just use \\d+ like here

PS : Suresh's answer is better than mine :P

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

Use lastIndexOf()

String result= yourUrl.substring(yourUrl.lastIndexOf('/') + 1);

Upvotes: 6

Related Questions