- 浏览: 96378 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (48)
- ruby (7)
- scala (1)
- java (11)
- jython (1)
- python (1)
- life (0)
- jruby (1)
- javascript (1)
- 翻译 (0)
- lua (2)
- SUSE (1)
- scheme (6)
- amb (1)
- 编译器 (0)
- javaee (1)
- 分布式 (0)
- 并发 (1)
- 杂感 (1)
- IO (0)
- POJO (0)
- EJB (0)
- concurrent (0)
- 算法 (1)
- ibm (0)
- JNI (0)
- AOP (0)
- asm (0)
- Groovy (1)
- agent (0)
- instrument (0)
- manifest (1)
- classloader (0)
- jboss (0)
- code (0)
- OSGI (0)
- 庄子 (0)
- JTA (0)
- SA (1)
- clojure (0)
- collections (0)
- hotswap (0)
- 序列化 (0)
- CORS (0)
- 停机 (0)
- JVM (0)
- parallel (0)
- NIO (0)
- weblogic (0)
- transaction (1)
- 反思 (1)
- 事务 (1)
- 海量数据 (0)
- JDO (0)
- JPA (0)
- storm (0)
- log4j (0)
- java2d (0)
- btrace (0)
- hadoop (1)
- cygwin (1)
- sshd (1)
- wanlu (0)
- mysql (0)
- debug (0)
- autotest (0)
- Error (0)
- 数据结构 (0)
- descriptor (0)
- jdb (0)
- hsdb (0)
- wiki (0)
- maven (0)
- spring (0)
- eclipse (0)
- mvc (0)
- 代理 (0)
- oracle (0)
- hibernate (1)
- Hash (0)
- blockingqueue (0)
- remember (0)
- graphviz (0)
- http (0)
- post (0)
- jdbc (0)
- websocket (0)
- inter (0)
- got error: The Network Adapter could not establish the connection when creating a data source in Weblogic pointing to a Oracle database (1)
- bug (0)
- xml (0)
- mail (0)
- db (0)
- JAXB (0)
- StAX (0)
- google (0)
- haskell (1)
- rpc (0)
- read-code (0)
- spring-cloud (0)
- service-registry-discovery (0)
最新评论
-
IamSungod:
很有探索精神,学过多种编程语言呀
clojure parse xml -
messi_18:
是的。不知道别的平台是否也有类似问题。
scalatest 尝试(一) -
llh110220:
lz在windows下编写的吧
scalatest 尝试(一)
String.split(String reg)这个方法一点不陌生。经常用:
结果是:
但是,如果改一下输入参数呢?
结果是:
这是你想要的吗?如果是,我不废话了。如果不是,那怎么办呢?
反正这不是我想要的,我想要:
我绕了一大圈子:我去找apache commons中有没有这样的实现,没有找到。于是我开始自己写一个:
我自以为可以了。于是开始测试这个方法,我用了一个复杂点的例子"a||b|c|"。还好JUnit绿了。好兴奋啊。这时候,我想不明白为何JDK不提供这么简单的实现,于是我又试了下:
结尾的那一项还是没了,但是中间的那个空字符串竟然还在。不会吧!难道?!我打开了jdk的api的页面。看到了还有这样一个方法:Pattern.split(CharSequence,int)。然后看到了这样一段话:
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
然后,我试了下
反思:
1>当你遇到一个问题,最优的解决方式就在最近的地方。
2>熟悉api是非常重要的。
3>不要手贱。
"a|b|c".split("\\|")
结果是:
["a","b","c"]
但是,如果改一下输入参数呢?
"a|b|".split("\\|")
结果是:
["a","b"]
这是你想要的吗?如果是,我不废话了。如果不是,那怎么办呢?
反正这不是我想要的,我想要:
["a","b",""]
我绕了一大圈子:我去找apache commons中有没有这样的实现,没有找到。于是我开始自己写一个:
static String[] newsplit(String str) { if (str == null || str.length() == 0) { return new String[0]; } List<Integer> indexes = new LinkedList<Integer>(); indexes.add(-1); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '|') { indexes.add(i); } } indexes.add(str.length()); List<String> list = new LinkedList<String>(); for (int i = 0; i < indexes.size()-1; i++) { list.add(str.substring(indexes.get(i)+1,indexes.get(i+1))); } return list.toArray(new String[0]); }
我自以为可以了。于是开始测试这个方法,我用了一个复杂点的例子"a||b|c|"。还好JUnit绿了。好兴奋啊。这时候,我想不明白为何JDK不提供这么简单的实现,于是我又试了下:
"a||b|c|".split("\\|")结果是:
["a","","b","c"]
结尾的那一项还是没了,但是中间的那个空字符串竟然还在。不会吧!难道?!我打开了jdk的api的页面。看到了还有这样一个方法:Pattern.split(CharSequence,int)。然后看到了这样一段话:
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
然后,我试了下
"a||b|c|".split("\\|",-1)结果就是我想要的。一刹那我想撞墙。为什么,一开始不仔细在jdk中先看看啊。
反思:
1>当你遇到一个问题,最优的解决方式就在最近的地方。
2>熟悉api是非常重要的。
3>不要手贱。
发表评论
-
Atomic reference vs volatile reference
2015-12-21 00:05 406volatile reference和atomic refer ... -
java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage
2013-06-27 16:27 0Today when I try to send mail b ... -
java exchanger
2013-06-19 15:54 0Today focus on: multi-thread in ... -
java blocking queue performance issue when large concurrency
2013-05-16 15:35 0I recalled someone ask me a que ... -
发现了几篇关于java虚拟机的好文章,要好好消化一下
2013-04-19 15:49 0好久没有关注jvm了,今天翻到了几篇好文章。先记下,有时间消化 ... -
Java Endorsed Standards Override Mechanism
2013-04-18 17:56 0Today I read an article named & ... -
java.lang.LinkageError
2012-12-11 14:13 0今天遇到了一个问题java.lang.IllegalAcces ... -
管理log插入。比如编译时插入。
2012-11-20 20:51 0管理log插入。比如编译时插入。 萌发这个想法是因为每次,调试 ... -
java解惑44题没重现
2012-11-09 02:18 0java解惑第44题我没有重现。 jdk1.6.0_35 ... -
java2d学习
2012-11-07 00:37 0想要系统的学习一下java2d 第一步学习画阿基米德螺旋曲线。 ... -
ASM初探
2012-11-02 00:38 0今天遇到了一个问题。Log4J的config and watc ... -
mina,hbase,zookeeper
2012-10-26 22:49 0我要学习探索新知识的有效手段。从这两个开始吧。 -
log4j那些事儿
2012-10-25 19:31 0都知道Log4j是用来输出日志的框架。不怕笑话,我一直都知道它 ... -
storm是什么
2012-09-28 16:49 0storm是什么?需要研究一下。 https://github ... -
你知道吗,-Long.MIN_VALUE == Long.MIN_VALUE
2012-09-20 22:58 3154相信吗,-Long.MIN_VALUE == Long.MIN ... -
transaction in JAVAEE and Spring
2012-09-14 19:16 0XA transaction and local transa ... -
weblogic 数据源的事务管理
2012-09-14 01:50 0有这样一个case,在weblogic下配置了两个数据源,其中 ... -
java NIO
2012-09-13 16:52 0由这篇文章想到了NIO。 然后又提到了这篇文章。 -
java 并发与并行(Java concurrent and parallel)
2012-09-13 16:19 0起因是想知道BlockedQueque是干嘛的,接着想到了这篇 ... -
java 序列化框架
2012-09-12 19:04 0读了这篇文章解开 phprpc 序列化性能高于 hessian ...
相关推荐
String.split()方法使用总结
功率分配因为有时String.split()还不够! 这是一个小型的Typescript库,我出于无奈而从头开始编写了命令行解析器。 它提供了一些实用程序方法来解决一些用例,而这些用例String.split()调用将无法解决。它能做什么?...
主要介绍了Java中String.split()用法小结的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
string[] sArray=s.Split('c') ; foreach(string i in sArray) Console.WriteLine(i.ToString()); 输出下面的结果: ab deab deab de 第二种方法 我们看到了结果是以一个指定的字符进行的分割。使用另一种构造方法对...
java中String类的intern、split方法的详细讲解。
柯里化的String.prototype.split支持按字符串、正则表达式或函数进行拆分。 npm install string-split --save npm 统计信息 例子 要求 var split = require ( 'string-split' ) ; 完整的应用程序 split ( "." , ...
下面小编就为大家带来一篇浅谈java String.split丢失结尾空字符串的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
java的String[] split 示例
主要介绍了String split方法实现过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
IE下的String.prototype.split()函数bug
将字符串按某个固定字符拆分成一个表结构(value,id) eg:1,2,3,3,4 拆分后: value id 1 1 2 2 3 3 3 4 4 5
replaceString)替换现有字符串. 100.string.split(分隔符)返回一个数组存储值. 101.string.substr(start[,length])取从第几位到指定长度的字符串. 102.string.toLowerCase()使字符串全部变为小写. 103....
string[] ss2 = classString.Split(new string[] { CConst.SPLITOR_CLASS_ITEM }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in ss2) _classList.Add(new CClass(s)); string[] ss3 = ...
本文实例讲述了python实现...list_of_string = string.split() print list_of_string #将字符串分离开,放入列表中 print '*'*50 def case_insensitive_sort(liststring): listtemp = [(x.lower(),x) for x in liststr
public static String[] split(String s, String regex) s参数为待拆分字符串, regex参数有两种格式: 单字符的字符串(长度1),功能如下:split(“ab#12#453”, “#”) 返回带5个元素的数组:ab, #, 12, #, 453 ...
关于string的split 和 join 方法对导入os模块进行os.path.splie()/os.path.join() 貌似是处理机制不一样,但是功能上一样。1.string.split(str=’ ‘,num=string.count(str)): 以str为分隔,符切片string,如果num...
split Split a string by the occurrences of a pattern. findall Find all occurrences of a pattern in a string. finditer Return an iterator yielding a match object for each match. compile Compile a ...
List<string> Old = new List<string>(oldCookie.Split(';')); List<string> New = new List<string>(newCookie.Split(';')); foreach (string n in New) { foreach (string o in Old) { if (o == n || o....