導航:首頁 > 編程語言 > java去掉前後的空格

java去掉前後的空格

發布時間:2022-07-15 18:01:39

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中如何去除字元串前後的全形空格(當中保留)

自己寫一個方法,使用split()去除角空格以及全形空格!~ split()返回是String[],後再組合成一個String

public String[] split(String regex)根據給定正則表達式的匹配拆分此字元串。
該方法的作用就像是使用給定的表達式和限制參數 0 來調用兩參數 split 方法。因此,所得數組中不包括結尾空字元串。

例如,字元串 "boo:and:foo" 使用這些表達式可生成以下結果:

Regex 結果
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }

參數:
regex - 定界正則表達式
返回:
字元串數組,它是根據給定正則表達式的匹配拆分此字元串確定的,,

根據你的要求重新編寫了以個程式,測試OK,
代碼如下:
class StrTest{
static String stringTest(String s){
int j=0,k=0,i=0;
char[] stra=new char[s.length()];
s.getChars(0,s.length(),stra,0);
for(i=0;i<s.length();i++){
if(stra[i]==' '||stra[i]==' '){
j=i+1;
}
else
{
break;
}
}
for(i=s.length()-1;i+1>0;i--){
if(stra[i]==' '||stra[i]==' '){
k=i;
}
else{
break;
}
}
String strb=new String(stra,j,k-j);
return strb;
}
public static void main(String[] args){
String str=new String(" a test test ");
System.out.println(stringTest(str));
}
}

你可以更改 String str=new String(" a test test "); 來測試

㈢ java如何刪除字元串的空格

java刪除字元串的空格:
1、刪除字元串前後的空格,使用字元串提供的方法trim()去除;例:String s=" aa "; s=s.trim(); s="aa";
2、刪除字元串前後的空格,使用字元串提供的方法replace()替換掉空格,該方法有兩個參數,參數一表示你要替換的目標字元串,參數二表示你要把目標字元串替換成什麼字元串;例:String s=" a a "; s=s.replace(" ",""); s="aa";我們把目標字元串空格,替換成空字元,就實現了去除空格

㈣ java中 什麼函數能去掉 字元串 後面的空格,只是後面的空格

String str = " asd ";
String ntr = ("A" + str).trim().substring(1);
System.out.println("str=\"" + str + "\"");
System.out.println("ntr=\"" + ntr + "\"");

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 可以匹配空格、製表符、換頁符等空白字元的其中任意一個

㈤ 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 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去空格

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();
}
閱讀全文

與java去掉前後的空格相關的資料

熱點內容
javaweb程序設計郭 瀏覽:239
gm聲望命令 瀏覽:484
pdf轉換器電腦版免費 瀏覽:41
解壓歌曲什麼歌最好 瀏覽:151
諾貝爾pdf 瀏覽:967
雲伺服器快速安裝系統原理 瀏覽:788
蘋果騰訊管家如何恢復加密相冊 瀏覽:115
手機軟體反編譯教程 瀏覽:858
sqlserver編程語言 瀏覽:650
gpa國際標准演算法 瀏覽:238
伺服器編程語言排行 瀏覽:947
怎麼下載快跑app 瀏覽:966
小紅書app如何保存視頻 瀏覽:170
如何解開系統加密文件 瀏覽:810
linux切換root命令 瀏覽:283
c編譯之後界面一閃而過怎麼辦 瀏覽:880
怎麼看ic卡是否加密 瀏覽:725
lgplc編程講座 瀏覽:809
cnc手動編程銑圓 瀏覽:724
cad中幾種命令的意思 瀏覽:327