在JAVA中从字符串(从url类型更改)中删除尾部斜杠

发布于 2024-10-26 20:45:47 字数 470 浏览 2 评论 0原文

我想从 Java 中的字符串中删除尾部斜杠。

我想检查字符串是否以 url 结尾,如果是,我想删除它。

这是我所拥有的:

String s = "http://almaden.ibm.com/";

s= s.replaceAll("/","");

和这个:

String s = "http://almaden.ibm.com/";
length  =  s.length();
--length;
Char buff = s.charAt((length);
if(buff == '/')
{
     LOGGER.info("ends with trailing slash");
/*how to remove?*/
}
else  LOGGER.info("Doesnt end with trailing slash");

但两者都不起作用。

I want to remove the trailing slash from a string in Java.

I want to check if the string ends with a url, and if it does, i want to remove it.

Here is what I have:

String s = "http://almaden.ibm.com/";

s= s.replaceAll("/","");

and this:

String s = "http://almaden.ibm.com/";
length  =  s.length();
--length;
Char buff = s.charAt((length);
if(buff == '/')
{
     LOGGER.info("ends with trailing slash");
/*how to remove?*/
}
else  LOGGER.info("Doesnt end with trailing slash");

But neither work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

会傲 2024-11-02 20:45:47

有两个选项: 使用模式匹配(稍慢):

s = s.replaceAll("/$", "");

或:

s = s.replaceAll("/\\z", "");

使用 if 语句(稍快):

if (s.endsWith("/")) {
    s = s.substring(0, s.length() - 1);
}

或(有点难看):

s = s.substring(0, s.length() - (s.endsWith("/") ? 1 : 0));

请注意,您需要使用 s = s...,因为字符串是不可变的。

There are two options: using pattern matching (slightly slower):

s = s.replaceAll("/$", "");

or:

s = s.replaceAll("/\\z", "");

And using an if statement (slightly faster):

if (s.endsWith("/")) {
    s = s.substring(0, s.length() - 1);
}

or (a bit ugly):

s = s.substring(0, s.length() - (s.endsWith("/") ? 1 : 0));

Please note you need to use s = s..., because Strings are immutable.

深爱不及久伴 2024-11-02 20:45:47

这应该效果更好:

url.replaceFirst("/*$", "")

This should work better:

url.replaceFirst("/*$", "")
旧人 2024-11-02 20:45:47

您可以使用 Apache Commons StringUtils 如下:

String s = "http://almaden.ibm.com/";
StringUtils.removeEnd(s, "/")

You can achieve this with Apache Commons StringUtils as follows:

String s = "http://almaden.ibm.com/";
StringUtils.removeEnd(s, "/")
╰◇生如夏花灿烂 2024-11-02 20:45:47

url.replaceAll("/$", "") $ 匹配字符串的末尾,因此它会替换尾部斜杠(如果存在)。

url.replaceAll("/$", "") the $ matches the end of a string so it replaces the trailing slash if it exists.

烛影斜 2024-11-02 20:45:47

java中的简单方法

String removeLastSlash(String url) {
    if(url.endsWith("/")) {
        return url.substring(0, url.lastIndexOf("/"));
    } else {
        return url;
    }
}

simple method in java

String removeLastSlash(String url) {
    if(url.endsWith("/")) {
        return url.substring(0, url.lastIndexOf("/"));
    } else {
        return url;
    }
}
挽袖吟 2024-11-02 20:45:47

正如其名称所示,replaceAll 方法将所有出现的搜索字符串替换为替换字符串。这显然不是你想要的。您可以通过阅读 javadoc 自己找到它。

第二个更接近事实。通过阅读 String 类的 javadoc,您将发现一个名为 substring 的有用方法,该方法在给定两个索引的情况下从字符串中提取子字符串。

As its name indicates, the replaceAll method replaces all the occurrences of the searched string with the replacement string. This is obviously not what you want. You could have found it yourself by reading the javadoc.

The second one is closer from the truth. By reading the javadoc of the String class, you'll find a useful method called substring, which extracts a substring from a string, given two indices.

巷雨优美回忆 2024-11-02 20:45:47

最简单的方法...

String yourRequiredString = myString.subString(0,myString.lastIndexOf("/"));

Easiest way ...

String yourRequiredString = myString.subString(0,myString.lastIndexOf("/"));
躲猫猫 2024-11-02 20:45:47

更紧凑的方式:

字符串路径示例 = "/test/dir1/dir2/";

String trimmedSlash = pathExample.replaceAll("^/|/$","");

regexp ^/ 替换第一个,/$ 替换最后一个

a more compact way:

String pathExample = "/test/dir1/dir2/";

String trimmedSlash = pathExample.replaceAll("^/|/$","");

regexp ^/ replaces the first, /$ replaces the last

迷鸟归林 2024-11-02 20:45:47

如果您是 Apache Commons Lang 的用户,您可以利用位于 StringUtils 中的 chomp 方法

字符串 s = "http://almaden.ibm.com/";

StringUtils.chomp(s,File.separatorChar+"")

If you are a user of Apache Commons Lang you can take profit of the chomp method located in StringUtils

String s = "http://almaden.ibm.com/";

StringUtils.chomp(s,File.separatorChar+"")

明月夜 2024-11-02 20:45:47

科特林方面

    fun removeTrailSlash(s: String): String {
        return s.replace(Regex("/$"), "")
    }

    fun String.removeTrailSlash(): String {
        return CommonUtil.removeTrailSlash(this)
    }
    @Test
    fun removeTrailSlash() {
        // given
        val expected = "asdf/qwer"
        val s = "$expected/"
        // when
        val actual = CommonUtil.removeTrailSlash(s)
        // then
        assertEquals(expected, actual)
    }
    
    @Test
    fun removeTrailSlash() {
        // given
        val expected = "asdf/qwer"
        val s = "$expected/"
        // when
        val actual = s.removeTrailSlash()
        // then
        Assertions.assertEquals(expected, actual)
    }

Kotlin side

    fun removeTrailSlash(s: String): String {
        return s.replace(Regex("/
quot;), "")
    }

    fun String.removeTrailSlash(): String {
        return CommonUtil.removeTrailSlash(this)
    }
    @Test
    fun removeTrailSlash() {
        // given
        val expected = "asdf/qwer"
        val s = "$expected/"
        // when
        val actual = CommonUtil.removeTrailSlash(s)
        // then
        assertEquals(expected, actual)
    }
    
    @Test
    fun removeTrailSlash() {
        // given
        val expected = "asdf/qwer"
        val s = "$expected/"
        // when
        val actual = s.removeTrailSlash()
        // then
        Assertions.assertEquals(expected, actual)
    }
以为你会在 2024-11-02 20:45:47
   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)
        }
    }  
   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)
        }
    }  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文