可以用String中的split方法分割字符串,使用这个方法后,得到的结果会保存在一个数组中 如:String a = "aa bb cc dd"; String b[] = a.split(" ");
用正则表达式就好 ^\s{2,}$ 即代表两个及以上的空格
可以用vbs dim a() a=split("长字符 串"," ") msgbox a
public class split{ public static void main(string[] args){ string sss = "1 2010,2 2011,3 2012"; string[] arr = sss.split(" |,");//根据“ ”和“,”区分 system.out.println(java.util.arrays.tostring(arr));//遍历输出数组 } }
思路:分割方式用正则表达式匹配;new String(s).split(" +")加号前边有一个空格表示空格出现一次或多次,这样就可以和任意个数的空格匹配了.例子:for(String s:"w s s sss s s s s qewe qweqw".split(" +"))System.out.println(s);运行结果:wsssssssssqeweqweqw
刚才没注意看,原来是有多个空格的,那你就用replaceaAll()先把一个或多个空格替换成英文输入法的逗号",",然后再用split()根据逗号","来分割数组就好了
public static void main(String args[]) { String target="John Bill Peter "; ArrayList list=new ArrayList(); for(String temp:target.split(" ")) { list.add(temp); } Collections.sort(list); for(String temp:list) { System.out.print(temp+" "); } }
public class temp{ public static void main(String[] args) { String temps ="aaa bbb ccc"; String arrays[] = temps.split(" "); for(int i=0;i 评论0 0 0
<pre t="code" l="java">public static void main(String[] args) { String s = "ab cde fg"; String[] re = s.split(" ");//用split()函数直接分割 for (String string : re) { System.out.println(string); } }用split()函数直接分割
#include <iostream>#include <vector>#include <string>using namespace std;int main(int argc, char* argv[]){ const int SIZE = 256; char szBuf[] = "This&is&a&sample&!"; char szFound[SIZE]; vector<string> vs; string str; int i=0; int k = 0; while (