0

How to remove characters between two specific characters in a String?

Example:

Original String: "Hello <>Remove this String<> how are you?"

Modified String: "Hello <><> how are you?"

3
  • 2
    Welcome on StackOverflow. Please read HowToAsk and then show what you tried
    – mpromonet
    Oct 18, 2021 at 19:08
  • 4
    The description seems a bit imprecise to me. I am not sure what should be done for example on a string like this: "aaaa<>bbbbb<>ccccc<>dddd".
    – CiaPan
    Oct 18, 2021 at 19:12
  • use StringBuilder to build a new String by using substring from the original String object. I'm sure there should be other ways too.
    – sevvalai
    Oct 18, 2021 at 19:51

1 Answer 1

1

You can use regex like this:

String result = original.replaceAll("<>.*<>", "<><>");

Output:

Hello <><> how are you?

1
  • 1
    "<>.*<>" as a 1st argument is shorter and achieves the same result
    – tucuxi
    Oct 18, 2021 at 19:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.