㈠ java怎樣去掉字元串內的空白
java去掉字元串內的空白有首發空白,中間空白及全部空白。可以根據需要通過下面的方法進行去掉。
方法如下:
1.String.trim()
trim()是去掉首尾空格
2.str.replace("","");去掉所有空格,包括首尾、中間
Stringstr="hello";
Stringstr2=str.replaceAll("","");
System.out.println(str2);
3.或者replaceAll("+","");去掉所有空格
4.str=.replaceAll("\s*","");
可以替換大部分空白字元,不限於空格
s可以匹配空格、製表符、換頁符等空白字元的其中任意一個
5.或者下面的代碼也可以去掉所有空格,包括首尾、中間
publicStringremove(Stringresource,charch)
{
StringBufferbuffer=newStringBuffer();
intposition=0;
charcurrentChar;
while(position
{
currentChar=resource.charAt(position++);
if(currentChar!=ch)buffer.append(currentChar);}returnbuffer.toString();
}
㈡ Java如何去除字元串中的空格、回車、換行符、製表符
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassStringUtils{
/**
*正則
*/
(Stringstr){
Stringdest="";
if(str!=null){
Patternp=Pattern.compile("\s*| | | ");
Matcherm=p.matcher(str);
dest=m.replaceAll("");
}
returndest;
}
publicstaticvoidmain(String[]args){
System.out.println(StringUtils.replaceBlank("justdoit!"));
}
/*-----------------------------------
笨方法:Strings="你要去除的字元串";
1.去除空格:s=s.replace('\s','');
2.去除回車:s=s.replace(' ','');
這樣也可以把空格和回車去掉,其他也可以照這樣做。
註: 回車(u000a)
水平製表符(u0009)
s空格(u0008)
換行(u000d)*/
}
㈢ java中去除字元串中 所有 的空格!
StringTokenizer這個類已經是Java不推薦使用的了。如果真想去除字元串所有空格,請用這個方法。
publicclassStringTo{
publicstaticvoidmain(String[]args){
Stringstring="Wearestudents";
System.out.println("原字元串是:");
System.out.println(string);
StringnewString=string.replaceAll("","");
System.out.println("去掉字元串所有空格後的字元串是:");
System.out.println(newString);
}
}
㈣ java字元串中去掉所有空白字元如何實現
//trim()是去除字元串開頭的空格的
Strings="asdaasassq112309asdlsiasa8s9";
System.out.println(s.replaceAll("",""));
㈤ JAVA怎麼【只】去掉字元串【前面的】空格
JAVA中去掉空格
1. String.trim() trim()是去掉首尾空格
2.str.replace(" ", ""); 去掉所有空格,包括首尾、中間
String str = " hell o "; String str2 = str.replaceAll(" ", ""); System.out.println(str2);
3.或者replaceAll(" +",""); 去掉所有空格
4.str = .replaceAll("\\s*", "");
可以替換大部分空白字元, 不限於空格 \s 可以匹配空格、製表符、換頁符等空白字元的其中任意一個
5.或者下面的代碼也可以去掉所有空格,包括首尾、中間
public String remove(String resource,char ch) {
StringBuffer buffer=new StringBuffer();
int position=0;
char currentChar;
while(position<resource.length()) {
currentChar=resource.charAt(position++);
if(currentChar!=ch) buffer.append(currentChar);
}
return buffer.toString();
}
㈥ Java如何去除字元串中的空格、回車、換行符、製表符
笨方法:String s = 你要去除的字元串;
1.去除空格:s = s.replace(‘\\s’,);
2.去除回車:s = s.replace(‘
’,);
這樣也可以把空格和回車去掉,其他也可以照這樣做。
註:
回車(\u000a)
\t 水平製表符(\u0009)
\s 空格(\u0008)
換行 將游標移動到下一行第一格 相當於平時用的回車 \r 回車 將游標移動到當前行第一格}
㈦ java怎樣去掉string中的空格,回車符
java中String有個trim()能夠去掉一個字元串的前後空格。
但是trim()只能去掉字元串中前後的半形空格,而無法去掉全形空格。
去掉全形空格需要在trim()方法的基礎上加上一些判斷。
String
textContent
="abctest";
textContent
=
textContent.trim();
while
(textContent.startsWith(""))
{//這里判斷是不是全形空格
textContent
=
textContent.substring(1,
textContent.length()).trim();
}
while
(textContent.endsWith(""))
{
textContent
=
textContent.substring(0,
textContent.length()
-
1).trim();
}
㈧ java去除字元串的空格(有條件的去除)
第一第三
/*trim()是去掉首尾空格 。具體的例子如下:*/
String strCom=" JA VA "; //定義字元串
String str=strCom.trim(); //去除字元串前後的空格
System.out.println("未去除前後空格的字元串:"+strCom);
System.out.println("去除前後空格後的字元串:"+str);
第二
/*str.replace(" ", ""); 去掉所有空格,包括首尾、中間*/
String str1 = " ";
String str2 = str1.replaceAll(" ", "");
System.out.println(str2+","+str2.length());
㈨ java去空格
java去掉空格,主要使用string的替換方法,如下:
1.String.trim()
trim()是去掉首尾空格
2.str.replace("","");去掉所有空格,包括首尾、中間
Stringstr="hello";
Stringstr2=str.replaceAll("","");
System.out.println(str2);
3.或者replaceAll("+","");去掉所有空格
4.str=.replaceAll("\s*","");
可以替換大部分空白字元,不限於空格
s可以匹配空格、製表符、換頁符等空白字元的其中任意一個
5.或者下面的代碼也可以去掉所有空格,包括首尾、中間
publicStringremove(Stringresource,charch)
{
StringBufferbuffer=newStringBuffer();
intposition=0;
charcurrentChar;
while(position<resource.length())
{
currentChar=resource.charAt(position++);
if(currentChar!=ch)buffer.append(currentChar);}returnbuffer.toString();
}