第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從Java字符串中去除前導和尾隨空格

從Java字符串中去除前導和尾隨空格

Qyouu 2019-10-08 10:15:51
是否有一種方便的方法可以從Java字符串中剝離任何前導或尾隨空格?就像是:String myString = "  keep this  ";String stripppedString = myString.strip();System.out.println("no spaces:" + strippedString);結(jié)果:no spaces:keep thismyString.replace(" ","") 將替換keep和this之間的空間。
查看完整描述

4 回答

?
繁花不似錦

TA貢獻1851條經(jīng)驗 獲得超4個贊

您可以嘗試trim()方法。


String newString = oldString.trim();

看看javadocs


查看完整回答
反對 回復 2019-10-08
?
qq_花開花謝_0

TA貢獻1835條經(jīng)驗 獲得超7個贊

trim()是您的選擇,但是如果您想使用replacemethod -可能更靈活,則可以嘗試以下操作:


String stripppedString = myString.replaceAll("(^ )|( $)", "");


查看完整回答
反對 回復 2019-10-08
?
慕少森

TA貢獻2019條經(jīng)驗 獲得超9個贊

現(xiàn)在,使用java-11,您可以利用String.stripAPI返回一個值為該字符串的字符串,并刪除所有前導和尾隨空格。相同的javadoc讀?。?/p>


/**

 * Returns a string whose value is this string, with all leading

 * and trailing {@link Character#isWhitespace(int) white space}

 * removed.

 * <p>

 * If this {@code String} object represents an empty string,

 * or if all code points in this string are

 * {@link Character#isWhitespace(int) white space}, then an empty string

 * is returned.

 * <p>

 * Otherwise, returns a substring of this string beginning with the first

 * code point that is not a {@link Character#isWhitespace(int) white space}

 * up to and including the last code point that is not a

 * {@link Character#isWhitespace(int) white space}.

 * <p>

 * This method may be used to strip

 * {@link Character#isWhitespace(int) white space} from

 * the beginning and end of a string.

 *

 * @return  a string whose value is this string, with all leading

 *          and trailing white space removed

 *

 * @see Character#isWhitespace(int)

 *

 * @since 11

 */

public String strip()

這些示例案例可能是:-


System.out.println("  leading".strip()); // prints "leading"

System.out.println("trailing  ".strip()); // prints "trailing"

System.out.println("  keep this  ".strip()); // prints "keep this"


查看完整回答
反對 回復 2019-10-08
  • 4 回答
  • 0 關(guān)注
  • 961 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號