java编程如何将字符串每2个分割并用空格隔开存入数组中? 用java如何实现将一些数字分割成每三个一对然后存入数组中 ...
JAVA\u4e2d \u6211\u8981\u5c06\u5b57\u7b26\u4e32\u4ee5\u7a7a\u683c\u4e3a\u6807\u8bb0\u5206\u5272\u6210\u591a\u4e2a\u5b57\u7b26\u4e32,\u5e76\u5c06\u5206\u5272\u540e\u7684\u5b57\u7b26\u4e32\u8bbe\u4e3a\u4e00\u4e2a\u6570\u7ec4split
public String[] split(String regex)\u6839\u636e\u7ed9\u5b9a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u62c6\u5206\u6b64\u5b57\u7b26\u4e32\u3002
\u8be5\u65b9\u6cd5\u7684\u4f5c\u7528\u5c31\u50cf\u662f\u4f7f\u7528\u7ed9\u5b9a\u7684\u8868\u8fbe\u5f0f\u548c\u9650\u5236\u53c2\u6570 0 \u6765\u8c03\u7528\u4e24\u53c2\u6570 split \u65b9\u6cd5\u3002\u56e0\u6b64\uff0c\u6240\u5f97\u6570\u7ec4\u4e2d\u4e0d\u5305\u62ec\u7ed3\u5c3e\u7a7a\u5b57\u7b26\u4e32\u3002
\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32 "boo:and:foo" \u4f7f\u7528\u8fd9\u4e9b\u8868\u8fbe\u5f0f\u53ef\u751f\u6210\u4ee5\u4e0b\u7ed3\u679c\uff1a
Regex \u7ed3\u679c
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
\u53c2\u6570\uff1a
regex - \u5b9a\u754c\u6b63\u5219\u8868\u8fbe\u5f0f
\u8fd4\u56de\uff1a
\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u5b83\u662f\u6839\u636e\u7ed9\u5b9a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u62c6\u5206\u6b64\u5b57\u7b26\u4e32\u786e\u5b9a\u7684
\u629b\u51fa\uff1a
PatternSyntaxException - \u5982\u679c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5\u65e0\u6548
\u4ece\u4ee5\u4e0b\u7248\u672c\u5f00\u59cb\uff1a
1.4
\u53e6\u8bf7\u53c2\u89c1\uff1a
Pattern
String str = "123648945819399529s";String[] arr = new String[str.length() / 3 == 0 ? str.length() / 3 : str.length() / 3 + 1];int index = 0;for(int i=0; i str.length()){arr[i] = str.substring(index);}else{arr[i] = str.substring(index,(index = index+3));}}//\u6253\u5370\u6570\u7ec4System.out.println(java.util.Arrays.toString(arr));
java将字符串每隔2个分割并用空格隔开存入数组的例子如下:
String str = "abcdefchakdjfkdjfks";//字符串
int m=str.length()/2;
if(m*2<str.length()){
m++;
}
String[] strs=new String[m];
int j=0;
for(int i=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));
}
可以先把字符串分割成char数字,然后循环存进去,中间对长度要判断准确,代码如下:
我只给出了封装好的方法,返回值是个字符串数组,你在主方法里调用就行,参数是要处理的字符串
private static String[] setStr(String str){
char[] aa=str.toCharArray();//分割字符串为char数组
int j=aa.length/2;//char数组长度整除2
int k=aa.length%2;//char数组长度对2取余,为了处理字符为奇数
int le=j+k;//定义字符串数组的长度
String[] bbb=new String[le];
for(int i=0;i<j;i++){//把字符两两存进去
bbb[i]=aa[2*i]+" "+aa[2*i+1];
}
if(k==1){
bbb[j]=aa[aa.length-1]+"";//字符长度为奇数时,最后一个存一个字母
}
for(int p=0;p<bbb.length;p++){
System.out.println(bbb[p]);
}
return bbb;
}
String str = "abcdefchakdjfkdjfks";
int m=str.length()/2;
if(m*2<str.length()){
m++;
}
String[] strs=new String[m];
int j=0;
for(int i=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));
}
字符串str =“abcdefchakdjfkdjfks”;
INT M = str.length()/ 2;
(M * 2 <str.length()){
M + +;
>}
的String [] STR基因座=新的String [M];
诠释J = 0;
(INT I = 0; <str.length(); i + +){
(i%2 == 0){
STR基因座[J] =“”+ str.charAt(I);
} {
STR基因座[J] = STR基因座[J] +“”+ str.charAt(I);
J + +;
}
}
System.out.println(Arrays.toString序列(STR)); }
用循环遍历
String.charAt(int index)
返回指定索引处的 char 值。
绛旓細Windows涓嬭鐢╘r\n锛屼互涓嬫槸浠g爜 --- String str="鎴戝緢鎯冲皢涓娈瀛楃涓浠庣涓涓瓧绗﹀紑濮嬪線鍚庢暟锛姣忛殧浜斾釜灏卞湪鍚庨潰鍔犱笂鎹㈣绗︼紝璇峰悇浣java楂樻墜甯繖锛岃阿璋";String str2 = "";for(int i = 0; i < str.length(); i += 5)if(i + 5 < str.length())str2 += str.substring(i, i +...
绛旓細浠g爜濡備笅:public class Test { public static void main(String[] args){ String s="12345543211234554321";StringBuffer s1=new StringBuffer(s);int index;for(index=5;index
绛旓細import java.util.Timer;import java.util.TimerTask;public class TenNumber{private static int index = 0;public static void main(String[] args){final String str = "鎬庝箞鐢java灏涓涓瀛楃涓叉瘡闅涓绉掓樉绀哄嚭涓涓瓧绗";Timer timer = new Timer();timer.schedule(new TimerTask(){@Override...
绛旓細浣犲彲浠ョ敤String绫婚噷闈㈢殑split("")鏂规硶鏉ユ媶鍒瀛楃涓 缁欎釜渚嬪瓙浣 锛歱ublic class Test {public static void main(String[] args) {String str = "12ljfs92";String[] ss = str.split("");for(int i = 0; i < ss.length; i++){System.out.print(ss[i] + " ");}}} ...
绛旓細瀛楃涓鍒囧垎鍙敤鍒皊plit鏂规硶锛涚ず渚嬩唬鐮侊細杈撳嚭缁撴灉锛氭敞鎰忎簨椤癸細1.瀛楃"|","*","+"閮藉緱鍔犱笂杞箟瀛楃锛屽墠闈㈠姞涓"\\"銆2.鑰屽鏋滄槸"\"锛岄偅涔堝氨寰楀啓鎴"\\\"銆3.濡傛灉涓涓瓧绗︿覆涓湁澶氫釜鍒嗛殧绗︼紝鍙互鐢"|"浣滀负杩炲瓧绗︺
绛旓細} } System.err.println(str1); return str1; } public static void main(String[] args) { String str1 = "涓婃捣閲戣瀺澶у帵"; String str2 = "涓婃腐闆嗗洟"; merge(str1, str2); }} 涓婇潰鐨勫仛娉曞彧鎶婂姞鍔犺繘鏉ョ殑瀛楃涓鍘婚噸锛岃屾湭鎶婂師鏉ョ殑瀛楃涓插幓閲嶅锛屼笅闈㈠仛浜嗕釜鏂扮殑 public static...
绛旓細浣犺繖鏍峰啓涓嶅锛岄鍏堜綘鐨刬鏄瘡娆″姞1锛屽氨鏄浣犵涓娆℃埅鐨勬槸1-10锛岀浜屾鎴殑灏辨槸2-11浜嗐傝繕鏈変綘杩欎釜i鐨勮寖鍥翠笉瀵癸紝浼氬鑷磇+10瓒呭嚭瀛楃涓鐨勯暱搴 String str="濡傛灉鐢ㄧ浉鍚岀殑绉嶅瓙鍒涘缓涓や釜Random瀹炰緥锛屽垯瀵规瘡涓疄渚嬭繘琛岀浉鍚岀殑鏂规硶璋冪敤搴忓垪锛屽畠浠皢鐢熸垚骞惰繑鍥炵浉鍚岀殑鏁板瓧搴忓垪涓Java浠123";String s[]=...
绛旓細浣犺幏鍙栨帶鍒跺彴杈撳叆鐨勬柟娉曢敊浜嗭紝搴旇鐢╪extLine()锛氳幏鍙栦笅涓琛岃緭鍏ョ殑鏁版嵁 import java.io.IOException;import java.util.Scanner;public class Test {public static void main(String[] args) throws IOException {Scanner sin = new Scanner(System.in);String str1 = sin.nextLine();String str2 = ...
绛旓細鐢╨ength()鏂规硶銆乧harAt鏂规硶缁撳悎for寰幆灏辫浜嗭紝package com.golden.test;public class TestString { public static void main(String[] args) { String str = "浣犲彨绁為┈锛熸垜鍙玊esson.";for (int i = 0; i < str.length(); i++) { char item = str.charAt(i);System.out.println(...
绛旓細substring(int beginIndex, int endIndex) 杩斿洖涓涓柊瀛楃涓锛屽畠鏄瀛楃涓茬殑涓涓瓙瀛楃涓层傝繖涓や釜閰嶅悎鐫浣跨敤灏卞彲浠ヨВ鍐冲惂 锛熷嚭鐜版壘鍒扮涓娆″嚭鐜扮殑浣嶇疆鎴彇锛岀劧鍚庡湪姣旇緝鏂扮殑锛屽仛涓鏁板櫒灏卞ソ浜嗐傛湁涓嶄細鐨勫彲浠ュ幓鏌ヤ笅JDK鏂囨。閲岄潰瑕佸鍑芥暟鍙互鐢紝濡傛灉娌℃湁鎴戝彲浠ヤ紶浣犮傜粰涓偖绠卞氨濂姐