Remove part of string in Java

后端 未结 12 1761
遥遥无期
遥遥无期 2020-11-28 04:22

I want to remove a part of string from one character, that is:

Source string:

manchester united (with nice players)

Target string:

12条回答
  •  没有蜡笔的小新
    2020-11-28 05:10

    If you just need to remove everything after the "(", try this. Does nothing if no parentheses.

    StringUtils.substringBefore(str, "(");
    

    If there may be content after the end parentheses, try this.

    String toRemove = StringUtils.substringBetween(str, "(", ")");
    String result = StringUtils.remove(str, "(" + toRemove + ")"); 
    

    To remove end spaces, use str.trim()

    Apache StringUtils functions are null-, empty-, and no match- safe

提交回复
热议问题