1.接口
-
public
interface
NoSplitInter {
-
public
abstract
void
nosplit
();
-
}
2.实现类
-
public
class
NoSplitImpl
implements
NoSplitInter {
-
private String str;
-
-
ArrayList<String> list =
new
ArrayList<String>();
-
-
public
NoSplitImpl
(String str) {
-
this.str = str;
-
}
-
-
@Override
-
public
void
nosplit
() {
-
//字符串如果不为空
-
while (!str.isEmpty()) {
-
//如果字符串中包含 剔除的字符串
-
if (str.contains(
"&&")) {
-
//存在 获得该字符串的下标 截取从0到下标的字符串
-
int
index
= str.indexOf(
"&&");
-
list.add(str.substring(
0, index));
-
-
// 然后将刚刚已经添加到集合中的部分通过切割舍去
-
str = str.substring(index +
2);
-
}
else {
-
//不存在 就把最后一部分添加进集合
-
list.add(str);
-
break;
-
}
-
}
-
System.out.println(list);
-
}
-
}
3.main方法
-
public
class
NOSplit {
-
public
static
void
main
(String[] args) {
-
// 多个不同的符号可以使用这个方法 \\w*\\w匹配字母数字
-
// String regex = "\\w*\\w";
-
// Pattern pattern = Pattern.compile(regex);
-
// Matcher matcher = pattern.matcher(str);
-
//
-
// while (matcher.find()) {
-
// System.out.print(matcher.group());
-
// }
-
-
String
str
=
"ab&&c";
-
NoSplitImpl
noSplit
=
new
NoSplitImpl(str);
-
noSplit.nosplit();
-
}
-
}
字符串组合
实现字符串组合,如:[“ab”,“2”]通过“&&”分隔符,组合成字符串“ab&&2
1.接口
-
public
interface
StrAddInter {
-
public
abstract
void
strAdd
();
-
}
2.实现类
-
public
class
StrAddImpl
implements
StrAddInter {
-
private String[] str;
-
-
//用来放合并后的字符串
-
private
String
newStr
=
"";
-
-
public
StrAddImpl
(String[] str) {
-
this.str = str;
-
}
-
-
@Override
-
public
void
strAdd
() {
-
for (String s : str) {
-
//输出为ab&&2&&
-
newStr += s.substring(
0, s.length()) +
"&&";
-
}
-
//需要截取后面的&&
-
System.out.println(newStr.substring(
0,newStr.length()-
2));
-
}
-
}
3.main方法
-
public
class
NOSplit {
-
public
static
void
main
(String[] args) {
-
String
str
=
"ab&&c";
-
String[] strArrs = {
"ab",
"2"};
-
-
System.out.println(Arrays.toString(strArrs));
-
StrAddImpl
strAdd
=
new
StrAddImpl(strArrs);
-
strAdd.strAdd();
-
}
-
public
class
NOSplit {
-
public
static
void
main
(String[] args) {
-
String
str
=
"ab&&c";
-
String[] strArrs = {
"ab",
"2"};
-
-
// 多个不同的符号可以使用这个方法 \\w*\\w匹配字母数字
-
// String regex = "\\w*\\w";
-
// Pattern pattern = Pattern.compile(regex);
-
// Matcher matcher = pattern.matcher(str);
-
//
-
// while (matcher.find()) {
-
// System.out.print(matcher.group());
-
// }
-
NoSplitImpl
noSplit
=
new
NoSplitImpl(str);
-
noSplit.nosplit();
-
System.out.println(
"--------------");
-
System.out.println(Arrays.toString(strArrs));
-
StrAddImpl
strAdd
=
new
StrAddImpl(strArrs);
-
strAdd.strAdd();
-
System.out.println(
"---------------");
-
-
char[] chars = str.toCharArray();
-
ArrayList<String> list =
new
ArrayList<>();
-
for (
char c : chars) {
-
System.out.println(c);
-
if (c !=
'&') {
-
list.add(String.valueOf(c));
-
}
-
}
-
System.out.println(list);
-
}
-
}