Java的split()方法分割字符串比较常用(见[Java]字符串以某特殊字符分割处理 ),但在有的时候,会遇到星号*等正则表达式中的特殊字符而无法分割的问题. 比如某需求,用户输入产品规格:厚*宽*长,在后台需拆分该规格字符串,如果写成如下代码则无法处理: String str="5*200*450"; String strs[]=str.split("*"); 对于星号这类特殊符号,要在前面加上\\,如: String str="5*200*450&…
java关于split分割字符串,空的字符串不能得到的问题   class T { public static void main(String args[]) { String num[] = new String[11]; String sLine = "101494|360103660318444|2008/06/17|周润英|1292.0|3085.76|2778.28|912.91|106.0|||"; num = sLine.split("\\|");…
Java字符串的split方法可以分割字符串,但和其他语言不太一样,split方法的参数不是单个字符,而是正则表达式,如果输入了竖线(|)这样的字符作为分割字符串,会出现意想不到的结果, 如, String str="中国|广东|深圳"; String[]location=str.split("|"); 那么location==["中","国","|","广","东"…
今天使用split分割"."的时候居然失败了,经过百度发现原来要加转义字符才行. 正确的写法: String test="1.2.3"; String[] s1=test.split("\\."); 结果: 如果不加转义就会分割失败,返回一个空的字符串数组. API中是这样写的: public String[] split(String regex) Splits this string around matches of the given r…
java字符串的split,只传一个参数,后面空白的字符串会被忽略: public static void main(String[] args) { String str = "ab|c||"; String [] split = str.split("\\|"); System.out.println(Arrays.toString(split)); } 输出结果是[ab, c]. 解决方法是split第二个参数传一个负数,例如 public static vo…
System.out.println(":ab:cd:ef::".split(":").length);//末尾分隔符全部忽略 System.out.println(":ab:cd:ef::".split(":",-1).length);//不忽略任何一个分隔符 System.out.println(StringUtils.split(":ab:cd:ef::",":").length)…
在Java中,一个String对象被一些特殊字符分隔时,可以使用split()方法,生成一个String[],然后进行其他的操作,就像下面这样: String str = "a1_b1_c1"; String[] strList = str.split("_"); 其实,split()的参数是一个正则表达式,当String对象是被正则表达式中的特殊字符分隔时,split()的参数就不能直接仅仅写这个特殊字符(不是不可以,只是得到的结果并不是我们想要的分隔结果),正则…
总结:正则表达式-- package com.c2; //写一个spli的用法,数字类 ===分割字符串 public class yqw { public static void main(String[] args) { String a = "192.168.43.130"; String c[] = a.split("\\.");// 数组 for (int i = 0; i < c.length; i++) { System.out.println(…
spilt方法作用 以所有匹配regex的子串为分隔符,将input划分为多个子串. 例如: The input "boo:and:foo", for example, yields the following results with these expressions: Regex Result :{ "boo", "and", "foo" } o { "b", "", "…
使用split分割时: String[] a="aa|bb|cc".split("|"); output: [a, a, |, b, b, |, c, c] 先看一下split的用法: String[] java.lang.String.split(String regex) Splits this string around matches of the given regular expression. This method works as if by in…