「split拼接java」split by

博主:adminadmin 2022-11-23 14:09:08 56

今天给各位分享split拼接java的知识,其中也会对split by进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Java中split的用法

这个字符串分割时用到的方法。

指定字符串按指定的标记进行分割,产生一个字符串数组。

例:str=“a,b,c”; String[] strs=str.split(","); //strs内就变为["a","b","c"]

对特殊符号需要进行注解,例如:“.”,用时应写为split("\\.")

我想用split(";")来分隔我的Java代码, 但是若是字符串中有`;`我该怎么办?

你的问题在于转义的双引号字符串内的分号不应该拿来分割。未解决这个问题只能将转义的部分单独截取出来,不参与分割,等其他部分分割结束后,再将转义的内容拼接进去即可。

为防止双引号转义符内部还有其他双引号转义符\"的影响,对\\\"用#@#占位符将之替换。

需要引入第三方工具类辅助进行占位符替换:

import org.apache.commons.lang3.StringUtils;

依赖为:

dependency

groupIdorg.apache.commons/groupId

artifactIdcommons-lang3/artifactId

version3.3.2/version

/dependency

代码为:

public class Main {

public static void main(String[] args) {

String a = "String a = \";\";String b = \"aaaa\";";

String b = "String a = \"\\\";\";String b = \"aaaa\";";

System.out.println(codeSplit(a));

System.out.println(codeSplit(b));

}

private static ListString codeSplit(String a){

//排除双引号转义符内部还有其他双引号转义符\"的影响,用“#@#”占位符将之替换

a = StringUtils.replace(a, "\\\"", "#@#");

ListString result = new ArrayList();

boolean start = true;//检查双引号转义符,为true时代表找到双引号转义符的左转义符,为false时代表找到双引号转义符的右转义符

while (a.indexOf("\"") 0) {

int index = a.indexOf("\"");//index:双引号左转义符位置

//发现双引号转义符时,将当前字符串的(0 至 index + 1)字符截取出来作为tmp

String tmp = a.substring(0, index + 1);

a = a.substring(index + 1);//将截取剩下的部分重新赋值给原字符串

if (start) {

//发现双引号转义符的左转义符,对tmp字符串按分号进行分割,将不为空的结果添加到结果集中

String[] stArr = tmp.split(";");

for (String s : stArr) {

if (s.length() 0) {

result.add(s);

}

}

} else {

//发现双引号转义符的右转义符,不进行分割,将tmp字符串拼接到结果集的最后一个字符串中

String end = result.get(result.size() - 1);

end += tmp;

result.remove(result.size() - 1);

result.add(end);

}

start = !start;//转置双引号转义符标志,正常情况下双引号转义符必须成对出现,否则将得到预期外的结果

}

//跳出循环后对剩余的a字符串再做一次分割

String[] stArr = a.split(";");

for (String s : stArr) {

if (s.length() 0) {

result.add(s);

}

}

//将结果集内的“#@#”占位符还原为\"

return result.stream().map(s - StringUtils.replace(s, "#@#", "\\\"")).collect(Collectors.toList());

}

}

运行结果:

java 如何将split拆分的数组拼接成字符串

public class Hello{

public static void main(String[] args){

String str = "a,b,c,d";

String[] arr = new String[str.replaceAll(",","").length()];

String result = "";

for(int i = 0; i  arr.length; i++){

arr[i] = str.replaceAll(",","").substring(i,(i+1));

}

for(int i = 0; i  arr.length; i++){

if(i == 0){

result += arr[i] + ",";

}else if(i == arr.length - 1){

result += arr[i];

}else{

result += "'" + arr[i] + "',";

}

}

System.out.println(result);

}

}

//运行结果:

a,'b','c',d

JAVA split("|") 简单的问题~

1、首先在电脑中打开JAVA,然后在项目中引入hutool的jar包。

2、接着定义一个String类型的字符串,如下图所示。

3、然后再定义一个int类型的开始位置,和一个int类型的长度,如下图所示。

4、然后String s = StrUtil.subWithLength(str,fromIndex,length);//从指定位置开始,截取指定长度的字符串。

5、最后运行程序查看测试结果,就完成了。

java里面的split方法

java和c#的split都差不多

以下是java的split的特性及一些例子:

java.lang.string.split

split 方法

将一个字符串分割为子字符串,然后将结果作为字符串数组返回。

stringObj.split([separator,[limit]])

stringObj

必选项。要被分解的 String 对象或文字。该对象不会被 split 方法修改。

separator

可选项。字符串或 正则表达式 对象,它标识了分隔字符串时使用的是一个还是多个字符。如果忽

略该选项,返回包含整个字符串的单一元素数组。

limit

可选项。该值用来限制返回数组中的元素个数。

说明:

split 方法的结果是一个字符串数组,在 stingObj 中每个出现 separator 的位置都要进行分解

。separator 不作为任何数组元素的部分返回。

示例1:

public class SplitDemo {

public static String[] ss = new String[20];

public SplitDemo() {

String s = "The rain in Spain falls mainly in the plain.";

// 在每个空格字符处进行分解。

ss = s.split(" ");

}

public static void main(String[] args) {

SplitDemo demo = new SplitDemo();

for (int i = 0; i ss.length; i++)

System.out.println(ss[i]);

}

}

程序结果:

The

rain

in

Spain

falls

mainly

in

the

plain.

示例2:

public class SplitDemo {

public static String[] ss = new String[20];

public SplitDemo() {

String s = "The rain in Spain falls mainly in the plain.";

// 在每个空格字符处进行分解。

ss = s.split(" ", 2);

}

public static void main(String[] args) {

SplitDemo demo = new SplitDemo();

for (int i = 0; i ss.length; i++)

System.out.println(ss[i]);

}

}

程序结果:

The

rain in Spain falls mainly in the plain.

示例3:

public class SplitDemo {

public static String[] ss = new String[20];

public SplitDemo() {

String s = "The rain in Spain falls mainly in the plain.";

// 在每个空格字符处进行分解。

ss = s.split(" ", 20);

}

public static void main(String[] args) {

SplitDemo demo = new SplitDemo();

for (int i = 0; i ss.length; i++)

System.out.println(ss[i]);

}

}

程序结果:

The

rain

in

Spain

falls

mainly

in

the

plain.

示例4:

public class SplitDemo {

public static void main(String[] args) {

String value = "192.168.128.33";

String[] names = value.split(".");

for (int i = 0; i names.length; i++) {

System.out.println(names[i]);

}

}

}

运行结果:

对,没看错!没有任何输出!

让我们来看看 split 方法的方法签名吧:

public string[] split(string regex)

这里的参数的名称是 regex ,也就是 regular expression (正则表达式)。这个参数并不是一个简单的分割用的字符,而是一个正则表达式,看了 split 方法的实现代码就更坚定了我们的信心:

public string[] split(string regex, int limit) {

return pattern.compile(regex).split(this, limit);

}

split 的实现直接调用的 matcher 类的 split 的方法。读者已经知道,“ . ”在正则表达式中有特殊的含义,因此我们使用的时候必须进行转义。

只要将

String[] names = value.split(".");

改为

String[] names = value.split("//.");

就可以了。

输出结果:

192

168

128

33

再加一点儿补充(这是Java帮助文档中的,更清晰一些):

public String[] split(String regex,int limit)根据匹配给定的正则表达式来拆分此字符串。

此方法返回的数组包含此字符串的每个子字符串,这些子字符串由另一个匹配给定的表达式的子字符串终止或由字符串结束来终止。数组中的子字符串按它们在此字符串中的顺序排列。如果表达式不匹配输入的任何部分,则结果数组只具有一个元素,即此字符串。

limit 参数控制模式应用的次数,因此影响结果数组的长度。如果该限制 n 大于 0,则模式将被最多应用 n - 1 次,数组的长度将不会大于 n,而且数组的最后项将包含超出最后匹配的定界符的所有输入。如果 n 为非正,则模式将被应用尽可能多的次数,而且数组可以是任意长度。如果 n 为零,则模式将被应用尽可能多的次数,数组可有任何长度,并且结尾空字符串将被丢弃。

例如,字符串 "boo:and:foo" 使用这些参数可生成下列结果:

Regex Limit 结果

: 2 { "boo", "and:foo" }

: 5 { "boo", "and", "foo" }

: -2 { "boo", "and", "foo" }

o 5 { "b", "", ":and:f", "", "" }

o -2 { "b", "", ":and:f", "", "" }

o 0 { "b", "", ":and:f" }

这种形式的方法调用 str.split(regex, n) 产生与以下表达式完全相同的结果:

Pattern.compile(regex).split(str, n)

参数:

regex - 定界正则表达式

limit - 结果阈值,如上所述

返回:

字符串数组,根据给定正则表达式的匹配来拆分此字符串,从而生成此数组

抛出:

PatternSyntaxException - 如果正则表达式的语法无效

从以下版本开始:

1.4

public String[] split(String regex)根据给定的正则表达式的匹配来拆分此字符串。

该方法的作用就像是使用给定的表达式和限制参数 0 来调用两参数 split 方法。因此,结果数组中不包括结尾空字符串。

例如,字符串 "boo:and:foo" 产生带有下面这些表达式的结果:

Regex 结果

: { "boo", "and", "foo" }

o { "b", "", ":and:f" }

参数:

regex - 定界正则表达式

返回:

字符串数组,根据给定正则表达式的匹配来拆分此字符串,从而生成此数组。

抛出:

PatternSyntaxException - 如果正则表达式的语法无效

split拼接java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于split by、split拼接java的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。