[Java] replace()、trim()

replace( ) 取代
Code:
public static void main(String[] args)
{
String s1 = "ABC123ABC123";
String s2 = "ABC 123";
String s3 = "ABC_123";

System.out.println(s1.replace("ABC","abc"));  //
System.out.println(s1);
System.out.println(s2.replace(" ",""));
System.out.println(s3.replace("_"," "));
}

輸出:
abc123abc123
ABC123
ABC 123
ABC123ABC123

trim( ) 去掉頭尾空白字元
Code:
public static void main(String[] args)
{
String s1 = "   123   ";
String s2 = "   abc  123  ";

System.out.println(s1. trim());
System.out.println(s2. trim()); //只會去掉前後的空白
}

輸出:
123
abc  123
By Hao★

留言

熱門文章

[C#] 將DataTable轉換成Html格式表格

[MS SQL] 查詢所有Procedure