// Use split() to extract substrings from a string.
class SplitDemo {
static void showSplit(String[] strs) {
for(String str : strs)
System.out.print(str + "|");
System.out.println("\n");
}
// Demonstrate split().
public static void main(String args[]) {
String result[];
// Split at spaces.
String testStr = "This is a test.";
System.out.println("Original string: " + testStr);
result = testStr.split("\\s+");
System.out.print("Split at spaces: ");
showSplit(result);
// Split on word boundaries.
testStr = "One, Two, and Three.";
System.out.println("Original string: " + testStr);
result = testStr.split("\\W+");
System.out.print("Split at word boundaries: ");
showSplit(result);
// Split same string on commas and zero or more spaces.
System.out.println("Original string: " + testStr);
result = testStr.split(",\\s*");
System.out.print("Split at commas: ");
showSplit(result);
// Split on word boundaries, but allow embedded
// periods and @.
testStr = "Jerry Jerry@HerbSchildt.com";
System.out.println("Original string: " + testStr);
result = testStr.split("[\\W && [^.@]]+");
System.out.print("Allow . and @ to be part of a word: ");
showSplit(result);
// Split on various punctuation and zero or more trailing spaces.
testStr = "This, is. a!:; test?";
System.out.println("Original string: " + testStr);
result = testStr.split("[.,!?:;]+\\s*");
System.out.print("Split on various punctuation: ");
showSplit(result);
}
}
输出:
Original string: This is a test.
Split at spaces: This|is|a|test.|
Original string: One, Two, and Three.
Split at word boundaries: One|Two|and|Three|
Original string: One, Two, and Three.
Split at commas: One|Two|and Three.|
Original string: Jerry Jerry@HerbSchildt.com
Allow . and @ to be part of a word: Jerry|Jerry@HerbSchildt.com|
Original string: This, is. a!:; test?
Split on various punctuation: This|is|a|test|
分享到:
相关推荐
split()分割字符串 1.不同环境下的区分 Java:分割字符串不能写成split(“$”)//$为要分割的字符 Android:分割字符串需要加上中括号split(“[$]”)//$为要分割的字符 2.特殊用法–当split()分割字符串遇上特殊...
这是我遇到的问题,然后发现pb里没有split方法,就自己写了一个,应该还能算是简单而实用吧
* 使用一个字符串分割另一个字符串 * * @param delimiter 边界上的分隔字符 * @param haystack 输入的字符串 * @param out 输出的字符串指针 * @return 分割成了多少个成员 */ int explode(char *delimiter, ...
delphi7分割字符串
c语言实现字符串分割split(), 实现C语言实现按分隔符来截取字符串
主要介绍了js使用split函数按照多个字符对字符串进行分割的方法,实例分析了split函数的使用技巧,非常具有实用价值,需要的朋友可以参考下
主要介绍了SQL Server实现split函数分割字符串功能及用法,结合实例形式分析了SQL Server实现split分割字符串的相关技巧与使用方法,需要的朋友可以参考下
pb split() pb 字符串分割函数 自己写的字符串分割函数
NULL 博文链接:https://onestopweb.iteye.com/blog/2358978
本文实例汇总了C++常用字符串分割方法,分享给大家供大家参考。具体分析如下: 我们在编程的时候经常会碰到字符串分割的问题,这里总结下,也方便我们以后查询使用。 一、用strtok函数进行字符串分割 原型: char *...
split()方法返回的字符串中的所有单词的列表,使用str作为分隔符(如果在未指定的所有空格分割),可选择限当前分割为数量num。 语法 以下是split()方法的语法: str.split(str=, num=string.count(str)). 参数 str...
C++分割字符串的实现 C++ split函数的实现方法
SQL FUNCTION Split 字符串分割函数 的创建,方法加实例
用于分割字符串
split()方法是一个非常强大的工具,可以帮助我们轻松地分割字符串,使其在各种应用中发挥重要作用。本文将介绍split()方法的用法,提供示例代码,详细解释其工作原理,并最终总结其重要性和用途。
NULL 博文链接:https://bijian1013.iteye.com/blog/2334967
lua字符串转数组,类似js里的split()和php里的explode()
C#中使用split分割字符串的几种方法,特整理下, 方便需要的朋友
下边的函数,实现了象数组一样去处理字符串。 一,用临时表作为数组 代码如下: create function f_split(@c varchar(2000),@split varchar(2)) returns @t table(col varchar(20)) as begin while(charindex(@split,@...
将字符串按某个固定字符拆分成一个表结构(value,id) eg:1,2,3,3,4 拆分后: value id 1 1 2 2 3 3 3 4 4 5