Remove part of string in Java

后端 未结 12 1744
遥遥无期
遥遥无期 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:06

    You could use replace to fix your string. The following will return everything before a "(" and also strip all leading and trailing whitespace. If the string starts with a "(" it will just leave it as is.

    str = "manchester united (with nice players)"
    matched = str.match(/.*(?=\()/)
    str.replace(matched[0].strip) if matched
    

提交回复
热议问题