導航:首頁 > 編程語言 > java字元串空格分割

java字元串空格分割

發布時間:2023-03-14 08:26:13

java如何把字元串用空格劃分

StringCount[]=string.split("\s+");
因為s表示空格必須轉義所以就是\s
+表示1個或者多個
所以連在一起就是\s+

運行測試

2001
Smith
10
4000

Ⅱ java編程:如何自行輸入數字來用空格分割字元串

這個可以啊

Scanner in=new Scanner(System.in);
System.out.println("輸入空格數:");
int n=in.nextInt();
String input = "TACDDFGGH????";
String output = "";
for(int i=0;i<input.length/n,i++)
{
output+=input.subString(i*n,(i+1)*n)+" ";

}
if(input%n!=0)
output+=input.subString(input.length-(input%n));
System.out.println (output);

Ⅲ Java 怎麼將一個字元串按空格分割,並每一部分放入一個數組中

publicstaticvoidmain(String[]args){
Strings="abcdefg";
String[]re=s.split("");//用split()函數直接分割
for(Stringstring:re){
System.out.println(string);
}
}

用split()函數直接分割

Ⅳ 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
{
currentChar=resource.charAt(position++);
if(currentChar!=ch) buffer.append(currentChar); } return buffer.toString();
}

Ⅳ java編程如何將字元串每2個分割並用空格隔開存入數組中

java將字元串每隔2個分割並用空格隔開存入數組的例子如下:

Stringstr="abcdefchakdjfkdjfks";//字元串
intm=str.length()/2;
if(m*2<str.length()){
m++;
}
String[]strs=newString[m];
intj=0;
for(inti=0;i<str.length();i++){
if(i%2==0){//每隔兩個
strs[j]=""+str.charAt(i);
}else{
strs[j]=strs[j]+""+str.charAt(i);//將字元加上兩個空格
j++;
}
}
System.out.println(Arrays.toString(strs));
}
閱讀全文

與java字元串空格分割相關的資料

熱點內容
只有程序員才懂的笑話 瀏覽:264
php開發app介面源碼 瀏覽:6
加密狗寶寶 瀏覽:231
單片機搶答器的原理 瀏覽:922
百利達體脂儀可以用什麼app連接 瀏覽:220
伺服器2016自動備份怎麼取消 瀏覽:838
dos攻擊命令大全 瀏覽:840
夢世界伺服器怎麼擴大領地 瀏覽:966
解壓breathe 瀏覽:431
webview調用java 瀏覽:346
android待機流程 瀏覽:854
python直接退出程序 瀏覽:847
百戰程序員收費標准 瀏覽:778
時鍾置換演算法指針變化規則 瀏覽:250
微信加密能否改密碼 瀏覽:109
android許可權組 瀏覽:175
2017單片機 瀏覽:481
讓孩子感興趣編程的電影 瀏覽:264
用顏料製作脆皮解壓球 瀏覽:938
火箭解壓器 瀏覽:78