在最后一次出现字符后删除字符串

时间:2021-09-05 07:47:47

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.

在我的应用程序中,我附加一个字符串来创建生成URL的路径。现在我想在按下后退按钮时删除附加的字符串。

Suppose this is the string:

假设这是字符串:

/String1/String2/String3/String4/String5

Now I want a string like this:

现在我想要一个这样的字符串:

/String1/String2/String3/String4/

How can I do this?

我怎样才能做到这一点?

5 个解决方案

#1


54  

You can use lastIndexOf() method for same with

你可以使用lastIndexOf()方法

if (null != str && str.length() > 0 )
{
    int endIndex = str.lastIndexOf("/");
    if (endIndex != -1)  
    {
        String newstr = str.substring(0, endIndex); // not forgot to put check if(endIndex != -1)
    }
}  

#2


37  

String whatyouaresearching = myString.substring(0, myString.lastIndexOf("/"))

#3


3  

Easiest way is ...

最简单的方法是......

        String path = "http://zareahmer.com/questions/anystring";

        int pos = path.lastIndexOf("/");

        String x =path.substring(pos+1 , path.length()-1);

now x has the value stringAfterlastOccurence

现在x的值为stringAfterlastOccurence

#4


3  

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

您可以使用org.apache.commons.lang3.StringUtils.substringBeforeLast,它是null安全的。

From the javadoc:

来自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:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8</version>
</dependency>

#5


0  

The third line in Nepster's answer should be

Nepster答案的第三行应该是

String x =path.substring(pos+1 , path.length());

String x = path.substring(pos + 1,path.length());

and not String x =path.substring(pos+1 , path.length()-1); since substring() method takes the end+1 offset as the second parameter.

而不是String x = path.substring(pos + 1,path.length() - 1);因为substring()方法将结束+ 1偏移量作为第二个参数。

#1


54  

You can use lastIndexOf() method for same with

你可以使用lastIndexOf()方法

if (null != str && str.length() > 0 )
{
    int endIndex = str.lastIndexOf("/");
    if (endIndex != -1)  
    {
        String newstr = str.substring(0, endIndex); // not forgot to put check if(endIndex != -1)
    }
}  

#2


37  

String whatyouaresearching = myString.substring(0, myString.lastIndexOf("/"))

#3


3  

Easiest way is ...

最简单的方法是......

        String path = "http://zareahmer.com/questions/anystring";

        int pos = path.lastIndexOf("/");

        String x =path.substring(pos+1 , path.length()-1);

now x has the value stringAfterlastOccurence

现在x的值为stringAfterlastOccurence

#4


3  

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

您可以使用org.apache.commons.lang3.StringUtils.substringBeforeLast,它是null安全的。

From the javadoc:

来自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:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8</version>
</dependency>

#5


0  

The third line in Nepster's answer should be

Nepster答案的第三行应该是

String x =path.substring(pos+1 , path.length());

String x = path.substring(pos + 1,path.length());

and not String x =path.substring(pos+1 , path.length()-1); since substring() method takes the end+1 offset as the second parameter.

而不是String x = path.substring(pos + 1,path.length() - 1);因为substring()方法将结束+ 1偏移量作为第二个参数。