2017-01-24 2 views
0

を強調する私はStringUtils.stripEndを使用して文字列のオフに末尾文字を削除しようとしている、と私は"FOO_FOO"から"_FOO"を除去しようとした場合、これは空の文字列を返します気づいたんです。例えば、期間でJava StringUtils.stripEnd、ハイフンまたは

Stripping '.BAR' from FOO.BAR --> FOO 
Stripping '.BAR' from BAR.BAR --> 
Stripping '_BAR' from FOO_BAR --> FOO 
Stripping '_BAR' from BAR_BAR --> 
Stripping '-BAR' from FOO-BAR --> FOO 
Stripping '-BAR' from BAR-BAR --> 

を出力

import org.apache.commons.lang3.StringUtils; 

public class StripTest { 

    public static void printStripped(String s1, String suffix){ 
     String result = StringUtils.stripEnd(s1, suffix); 
     System.out.println(String.format("Stripping '%s' from %s --> %s", suffix, s1, result)); 
    } 

    public static void main(String[] args) { 
     printStripped("FOO.BAR", ".BAR"); 
     printStripped("BAR.BAR", ".BAR"); 
     printStripped("FOO_BAR", "_BAR"); 
     printStripped("BAR_BAR", "_BAR"); 
     printStripped("FOO-BAR", "-BAR"); 
     printStripped("BAR-BAR", "-BAR"); 
    } 

} 

は、誰かがこの動作を説明できますか?この場合のexamples from docsは見られませんでした。 StringUtils Javadocの中に存在ドキュメントと例でのJava 7

+0

文書では、「任意の**文字セット**を削除します」と表示されます。文字列ではありません。したがって、 '' .BAR ''を取り除くと、文字列の最後から ''すべて ''、 '' B'、 'A'と' R'文字を取り除くと言っています。 – khelwood

+0

@khelwoodそのコメントを回答してください:) –

+0

docの例を見てください。 'StringUtils.stripEnd(" 120.00 "、" .0 ")=" 12 "'代わりに、あなたは 'removeEnd(...)' 'ソース文字列の終わりにある場合にのみ部分文字列を削除し、そうでなければソース文字列を返します。' – Brendan

答えて

1

ルックを使用する:それはどこにでも最後から文字のセットを取り除くことができますよう

Strips any of a set of characters from the end of a String. 

A null input String returns null. An empty string ("") input returns the empty string. 

If the stripChars String is null, whitespace is stripped as defined by Character.isWhitespace(char). 

StringUtils.stripEnd(null, *)   = null 
StringUtils.stripEnd("", *)   = "" 
StringUtils.stripEnd("abc", "")  = "abc" 
StringUtils.stripEnd("abc", null)  = "abc" 
StringUtils.stripEnd(" abc", null) = " abc" 
StringUtils.stripEnd("abc ", null) = "abc" 
StringUtils.stripEnd(" abc ", null) = " abc" 
StringUtils.stripEnd(" abcyx", "xyz") = " abc" 
StringUtils.stripEnd("120.00", ".0") = "12" 

これは、あなたが望むものではありません。私はあなたがremoveEnd(...)

Removes a substring only if it is at the end of a source string, otherwise returns the source string. 

A null source string will return null. An empty ("") source string will return the empty string. A null search string will return the source string. 

StringUtils.removeEnd(null, *)  = null 
StringUtils.removeEnd("", *)  = "" 
StringUtils.removeEnd(*, null)  = * 
StringUtils.removeEnd("www.domain.com", ".com.") = "www.domain.com" 
StringUtils.removeEnd("www.domain.com", ".com") = "www.domain" 
StringUtils.removeEnd("www.domain.com", "domain") = "www.domain.com" 
StringUtils.removeEnd("abc", "") = "abc" 

removeEnd(...)文字のセットが、代わりにあなたが抽出しようとしているものである部分文字列ではなく、動作を探していると信じています。

+0

ありがとう、それをもっとはっきりと読んでいるはずです。 'stripEnd'に' www.domain.com'の例があることを明確な例として示してください。 '120.00 'にはそれがありますが、それは明確ではありません。 –

+0

しかし、私はあなたが探している正確な(十分に近い)タイプのケースをカバーしていると信じています。 – Brendan

関連する問題