上次出现字符后删除字符串 [英] Remove string after last occurrence of a character

查看:100
本文介绍了上次出现字符后删除字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

假设这是字符串:

/String1/String2/String3/String4/String5

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

Now I want a string like this:

/String1/String2/String3/String4/

我该怎么做?

推荐答案

您可以将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)
    }
}  

这篇关于上次出现字符后删除字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆