如何通过split()或matcher将文本拆分为android textview。当我拆分文本时,我想同时保留分隔符。我用过
Pattern stuff = Pattern.compile("[\\s]|[\\w+]");和
split("(?<!^|[a-zA-Z'])|(?![a-zA-Z'])")但是结果并不是我所期望的。它不会像android中的textview那样拆分
示例可以是任意文本:
Lorem Ipsum很简单!印刷排版行业的虚拟文本。自15世纪以来,Lorem Ipsum一直是业界的标准虚拟文本,当时一家不知名的印刷商拿出了一个排版,然后把它弄乱了,做成了一个排版样本簿。它不仅存活了五个世纪,还经历了电子排版的飞跃,基本上保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset sheets的发布而流行起来,最近又随着包括Lorem Ipsum版本的桌面出版软件Aldus PageMaker而流行起来。
发布于 2015-05-20 06:33:15
我假设您希望将文本拆分成句子,如下所示
Lorem Ipsum is simply
dummy text of the printing and typesetting industry
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum我使用了字符串的分裂函数,如下所示。
String[] results = source.split("!|\\.|\\?");希望能有所帮助。
另外,如果您想保留分隔符,下面是示例。
String[] results = source.split("!|\\.|\\?");
int offset = 0;
for(String result : results){
offset += result.length();
// just append the delimiter after the split sub string.
Log.d("", "result = " + result + source.charAt(offset));
offset +=1;
}https://stackoverflow.com/questions/30331704
复制相似问题