Java - 从字符串中修剪前导或尾随字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25691415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:56:00  来源:igfitidea点击:

Java - Trim leading or trailing characters from a string?

javatrim

提问by Brad Parks

How can I trim the leading or trailing characters from a string in java?

java - 如何从java中的字符串中修剪前导或尾随字符?

For example, the slash character "/" - I'm not interested in spaces, and am looking to trim either leading or trailing characters at different times.

例如,斜杠字符“/” - 我对空格不感兴趣,并且希望在不同时间修剪前导或尾随字符。

采纳答案by Reimeus

You could use

你可以用

Leading:

领导:

System.out.println("//test/me".replaceAll("^/+", ""));

Trailing:

尾随:

System.out.println("//test/me//".replaceAll("/+$", ""));

回答by Brad Parks

You can use Apache StringUtils.stripStartto trim leading characters, or StringUtils.stripEndto trim trailing characters.

您可以使用 Apache StringUtils.stripStart修剪前导字符,或使用StringUtils.stripEnd修剪尾随字符。

For example:

例如:

System.out.println(StringUtils.stripStart("//test/me", "/"));

will output:

将输出:

test/me

考验我

Note that if for some reason you can't use the whole StringUtils library, you could just rip out the relevant parts, as detailed here:

请注意,如果由于某种原因您不能使用整个 StringUtils 库,您可以删除相关部分,详情如下:

回答by Andrushenko Alexander

My version of trimming leading and/or trailing String s from String str. Both arguments may be null. When str does not has leading and/or trailing s, it is not changed.

我从 String str 修剪前导和/或尾随 String 的版本。两个参数都可以为空。当 str 没有前导和/或尾随 s 时,它不会改变。

String trim(String str, String s) {
    String res = s == null ? str : str == null ? null : s.length() >= str.length() ? str : str.replaceFirst(s, "");
    if ((res != null) && (s != null) && (res.length() >= s.length())) {
        return res.substring(res.length() - s.length(), res.length()).equals(s) ? res.substring(0, res.length() - s.length()) : res;
    }
    return res;
}

回答by Oleg

You could use a simple iteration if you want to remove the leading characters from a string :

如果要从字符串中删除前导字符,可以使用简单的迭代:

String removeLeadingChar(String s, char c) {
    int i;
    for(i = 0; i < s.length() && s.charAt(i) == c; ++i);
    return s.substring(i);
}

same logic applies if you want to remove any trailing char.

如果您想删除任何尾随char.

回答by Minhas Kamal

Trim with Character, String, or Regex

用字符、字符串或正则表达式修剪

If run-time is not a big issue for you, then this code will prove really helpful.

如果运行时对您来说不是大问题,那么此代码将证明非常有用。

public class StringTrimmer {
    public static String trim(String string, char ch){
        return trim(string, ch, ch);
    }

    public static String trim(String string, char leadingChar, char trailingChar){
        return string.replaceAll("^["+leadingChar+"]+|["+trailingChar+"]+$", "");
    }

    public static String trim(String string, String regex){
        return trim(string, regex, regex);
    }

    public static String trim(String string, String leadingRegex, String trailingRegex){
        return string.replaceAll("^("+leadingRegex+")+|("+trailingRegex+")+$", "");
    }

    // test
    public static void main(String[] args) {
        System.out.println(trim("110100", '1', '0')); // outputs: 01
        System.out.println(trim("**Aa0*#**", '*')); // outputs: Aa0*#
        System.out.println(trim("123##22222", "12", "22")); // outputs: 3##2
        System.out.println(trim("101101##10101", "101")); // outputs: ##10
        System.out.println(trim("123##abcde", "\d", "[c-e]")); // outputs: ##ab
    }
}

回答by Gabriel Gates

For those using Spring:

对于使用 Spring 的用户:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/StringUtils.html#trimTrailingCharacter-java.lang.String-char-

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/StringUtils.html#trimTrailingCharacter-java.lang.String-char-

import org.springframework.util.StringUtils;    

public void setFileName(String fileName) {
    // If the extension does not exist trim the trailing period
    this.fileName = StringUtils.trimTrailingCharacter(fileName,'.');
}