publicclassSplitExample2{publicstaticvoidmain(Stringargs[]){Stringstr="My name is Chaitanya";//regular expression is a whitespace hereString[]arr=str.split(" ");for(Strings:arr)System.out.println(s);}} Java Copy 输出: Myname isChaitanya Java Copy
Stringstr="how to do-in-java-provides-java-tutorials";String[]strArray=str.split("-");//[how to do, in, java, provides, java, tutorials] 2.2. Split by Whitespace The following Java program splits a string by space using the delimiter"\\s". To split by all white space characters (...
可以使用Character Class中的isWhitespace函数删除空格。 public static void main(String[] args) { String withSpace = "Remove white space from line"; StringBuilder removeSpace = new StringBuilder(); for (int i = 0; i<withSpace.length();i++){ if(!Character.isWhitespace(withSpace.charAt(i)))...
String.replaceFirst String.split 下面是例子代码 publicclassBlog {publicstaticvoidmain(String[] args) { String s0= "ccc123aaa456bbb"; System.out.println(s0.matches(".*\\d+.*")); System.out.println(newString(s0).replaceAll("\\d", "-")); System.out.println(newString(s0).replaceFirst("...
public static String jsGetVal(String objectString) { StringBuilder result = new StringBuilder(); StringBuilder val = new StringBuilder(); String[] vals = split(objectString, "."); for (int i = 0; i < vals.length; i++) { val.append("." + vals[i]); result.append("!" + (val.su...
row?'':!row.user?'':!?'': */ public static String jsGetVal(String objectString) { StringBuilder result = new StringBuilder(); StringBuilder val = new StringBuilder(); String[] vals = split(objectString, "."); for (int i = 0; i < vals.length; i++) { val.append("." + vals...
Map<String, String> splitterMap = Splitter.on(";").withKeyValueSeparator("-").split("a-1;b-2"); System.out.println(Joiner.on("|").withKeyValueSeparator("=").join(splitterMap)); } 注:splitter实例总是不可变的。用来定义splitter目标语义的配置方法总会返回一个新的splitter实例。这使得splitte...
String[]split(String regex) Splits this string around matches of the given regular expression. String[]split(String regex, int limit) Splits this string around matches of the given regular expression. booleanstartsWith(String prefix) Tests if this string starts with the specified prefi...
StringTokenizer st=new StringTokenizer(str,","); while(st.hasMoreTokens()) { System.out.println(st.nextToken()); } Guava Splitter Splitter使用令人放心的、直白的流畅 API 模式对这些混乱的特性作了完全的掌控。 Iterable<String> split = Splitter.on(',') ...
StringblogName="how,to,do,in,java";String[]tokenArray=blogName.split(",");//["how", "to", "do", "in", "java"] We need to modify the regex expression for any additional requirements. Such for ignoring the whitespaces around commas, we can use the pattern “\\s,\\s”. ...