user3734011
user3734011

Reputation: 15

How to cut everything before specific characters

Good Morning, I'm new in Java and I'd like to cut this String:

"jdbc:hsqldb:mem:testdb" 

into this:

"testdb"

I've tryed it with regex, but how I said, I'm new and I don't know the Syntax of regex very good. I tried something like this:

databaseName = databaseName.replaceAll("*.:", "");
System.out.println(databaseName);

It would be glad if someone could help me. Thank you for your answers

Upvotes: 0

Views: 232

Answers (3)

Baby
Baby

Reputation: 5092

As @MadProgrammer said in his comment, "there's more than one way to skin this fish...".

If you wish to skin this fish using regex, This will work:

databaseName = databaseName.replaceAll(".*(?=:)?:","");

Upvotes: 2

MadProgrammer
MadProgrammer

Reputation: 347194

If you know you only what everything after the last : then you can simply use

databaseName = databaseName.substring(databaseName.lastIndexOf(":") + 1)

Of course you should make sure it actually contains the delimiter...

if (databaseName.contains(":")) {...

You could also use String#split and grab the elements you want...

String parts[] = databaseName.split(":");
String name = parts[parts.length - 1];

Upvotes: 4

TheLostMind
TheLostMind

Reputation: 36304

String[] str = yourString.split(":");
//str[str.length -1] is your answer.

Upvotes: 3

Related Questions