5

How do I get the substring for the string :

 "s3n://bucket/test/files/*/*"

I would like to get s3n://bucket/test/files alone. I tried the split :

"s3n://bucket/test/files/*/*".split("/*/*") but this gives me array of strings with each character.

2 Answers 2

7

The argument to split is a regex and /*/* matches all the characters in your string. You need to escape *:

"s3n://bucket/test/files/*/*".split("/\\*/\\*")

An alternative to split in this case could be:

"s3n://bucket/test/files/*/*".stripSuffix("/*/*")
Sign up to request clarification or add additional context in comments.

Comments

6

A couple of options not using a regex.

Using takeWhile gives "s3n://bucket/test/files/" which includes the last slash.

scala> s.takeWhile(_ != '*')
res11: String = s3n://bucket/test/files/

Using indexOf to find the first "*" and taking one less character than that gives the output you specify.

scala> s.slice(0,s.indexOf("*") - 1)
res14: String = s3n://bucket/test/files

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.