英文:
I want to split string by number and symbol "." in java
问题
"我需要按numbers
和.
符号拆分字符串。我的字符串是
"Nagapur82. Nanavade83. Nandur84. Nandurkichi Wadi85."
我想要一个类似的数组
[ "Nagapur", "Nanavade", "Nandur", "Nandurkichi Wadi"]
```"
<details>
<summary>英文:</summary>
I need to split the String by `numbers` and `.` symbol.
My String is
"Nagapur82. Nanavade83. Nandur84. Nandurkichi Wadi85."
and I want Array of like
[ "Nagapur", "Nanavade", "Nandur", "Nandurkichi Wadi"]
</details>
# 答案1
**得分**: 0
```java
// 让我们首先手动实现一些东西,只是为了好玩。
// 首先,你要计算子字符串的数量:
int count = 0; // 元素的数量
boolean wasSeparator = true;
for (int index = 0; index < s.length(); index++) {
boolean isSeparator = (((Character.isDigit(s.charAt(index))) || (s.charAt(index) == '.')));
if (wasSeparator && (!isSeparator)) {
count++;
}
wasSeparator = isSeparator;
}
// `count` 代表了数组的长度。现在将子字符串计算到一个字符串数组中:
String[] elements = new String[count];
int itemIndex = 0;
int start = -1;
int end = -1;
for (int index = 0; index < s.length(); index++) {
boolean isSeparator = (((Character.isDigit(s.charAt(index))) || (s.charAt(index) == '.')));
if (isSeparator) {
if (start >= 0) {
end = index;
elements[itemIndex++] = s.substring(start, end);
start = -1;
end = -1;
}
} else {
if (start == -1) start = index;
}
}
if (start >= 0) elements[itemIndex++] = s.substring(start);
// 当然,你可以使用 `split` 方法:
String[] result = s.split(yourregex);
// 你的正则表达式是数字或点。你的正则表达式将包含 \d 和 \。
// 在这里,你的正则表达式是 `(\d|\.)+`,所以尝试:
String[] result = s.split("(\\d|\\.)+");
英文:
Let's implement something manually first, just for fun.
First, you count the number of substrings:
int count = 0; //Number of elements
boolean wasSeparator = true;
for (int index = 0; index < s.length(); index++) {
boolean isSeparator = (((Character.isDigit(s.charAt(index))) || (s.charAt(index) == '.')));
if (wasSeparator && (!isSeparator) {
count++;
}
wasSeparator = isSeparator;
}
count
represents the length of your array. Now compute the substrings into an array of strings:
String[] elements = [];
int itemIndex = 0;
int start = -1;
int end = -1;
for (int index = 0; index < s.length; index++) {
boolean isSeparator = (((Character.isDigit(s.charAt(index))) || (s.charAt(index) == '.')));
if (isSeparator) {
if (start >= 0) {
end = index;
elements[itemIndex++] = s.substring(start, end);
start = -1;
end = -1;
}
} else {
if (start == -1) start = index;
}
}
if (start >= 0) elements[itemIndex++] = s.substring(start);
Of course, you can use split
:
String[] result = s.split(yourregex);
and your regex is that it's digit or dot. Your regular expression will contain \d and .
Your regex here is (\d|\.)+
, so try
String[] result = s.split((\d|\.)+);
答案2
得分: 0
String s = "Nagapur82. Nanavade83. Nandur84. Nandurkichi Wadi85.";
s = s.replace(" ", "");
s = s.replace(".", "");
String[] s2 = s.split("[0-9]+");
This may work
英文:
String s = "Nagapur82. Nanavade83. Nandur84. Nandurkichi Wadi85.";
s = s.replace(" ", "");
s = s.replace(".", "");
String[] s2 = s.split("[0-9]+");
This may work
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论