0

I'm a newbie in java and as far as I concern this is pretty to do simple but I don't remember exactly the synthax I need to apply in order to get what's next:

I have the following string:

Random sentence with a few words

I want a proper synthax which returns the following:

Random-sentence-with-a-few-words

Thanks.

1
  • 2
    replace whitespace with hyphen? There is a String.replace() method for that. Commented Apr 9, 2014 at 19:47

2 Answers 2

3

Use String#replace(char, char):

myRandomStringWithSpaces.replace(' ', '-');

Edit: Used chars instead as per linked javadoc

Sign up to request clarification or add additional context in comments.

Comments

0

You can use a replace

Use replace() to replace specifics words in your string:
MyString.replace(' ', '-');

Here you need specify 2 chars (the original value and the second parameter the value to replace)

You can read more about it here

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.