1

I have a string here:

javax.swing.JLabel[,380,30,150x25,alignmentX=0.0,alignmentY=0.0]: Hello

I want to remove everything before the ":", including the ":" itself. This would leave only "Hello". I read about regex, but no combination I tried worked. Can someone tell me how to do it. Thanks in advance!

2 Answers 2

3

You need to use replaceAll method or replaceFirst.

string.replaceFirst(".*:\\s*", "");

or

string.replaceAll(".*:\\s*", "");

This would give you only Hello. If you remove \\s* pattern,then it would give you <space>Hello string.

  • .* Matches any character zero or more times, greedily.
  • : Upto the colon.
  • \\s* Matches zero or more space characters.
Sign up to request clarification or add additional context in comments.

Comments

1

You could also just split the string by : and take the second string. Like this

String sample = "javax.swing.JLabel[,380,30,150x25,alignmentX=0.0,alignmentY=0.0]: Hello";

System.out.println(sample.split(":", -1)[1]);

This will output

<space>Hello

If you want to get rid of that leading space just trim it off like

System.out.println(sample.split(":", -1)[1].trim());

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.