超初我用的只是最簡單的
網上搜索的都是什麼replace(" "
你使用replaceAll(" "
代碼如下
String str = "A B C D E";
str = str
這樣就刪除空格了
後來網站搜索發現如下代碼
代碼如下
import java
import java
public class StringUtil {
public static void replaceBlank()
{
Pattern p = Pattern
String str="I am a
System
Matcher m = p
String after = m
System
}
public static void main(String[] args) {
replaceBlank();
}
}
上面這代碼可以字符串中的空格
我再google一下
代碼如下
package com
import java
import java
import java
public class DelSpace {
public static void main(String[] args){
String str = " wo shi zhong guo ren ";
sameResult(str);
}
private static void sameResult(String str) {
System
System
System
System
}
public static String delByPattern(String str){
Pattern p=Pattern
Matcher m=p
String result=m
return result;
}
private static String delByRegex(String str) {
String[] arr = str
String result = "";
for(int i =
result+=arr[i]+" ";
}
if(!str
result=result
}
return result;
}
public static String delBySB(String str){
StringBuffer sb = new StringBuffer(str);
for(int index =
if(index<sb
sb
index
}
}
return sb
}
private static String delByArr(String str) {
char[] arr = str
String result = "";
ArrayList<Character> al = new ArrayList<Character>();
for(int i=
if(i<(arr
continue;
}
else{
al
}
}
al
for(int i=
result+=al
}
return result;
}
}
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20621.html