Remove string after last occurrence of a character

前端 未结 5 2079
慢半拍i
慢半拍i 2020-12-01 07:41

In my application, I am appending a string to create path to generate a URL. Now I want to remove that appended string on pressing back button.

Suppose this is the s

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 08:00

    You can use org.apache.commons.lang3.StringUtils.substringBeforeLast which is null-safe.

    From the javadoc:

    // The symbol * is used to indicate any input including null.
    StringUtils.substringBeforeLast(null, *)      = null
    StringUtils.substringBeforeLast("", *)        = ""
    StringUtils.substringBeforeLast("abcba", "b") = "abc"
    StringUtils.substringBeforeLast("abc", "c")   = "ab"
    StringUtils.substringBeforeLast("a", "a")     = ""
    StringUtils.substringBeforeLast("a", "z")     = "a"
    StringUtils.substringBeforeLast("a", null)    = "a"
    StringUtils.substringBeforeLast("a", "")      = "a"
    

    Maven dependency:

    
        org.apache.commons
        commons-lang3
        3.8
    
    

提交回复
热议问题