我想在Java中按数字和符号“.”拆分字符串。

huangapple go评论138阅读模式
英文:

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 &lt; s.length(); index++) {
    boolean isSeparator = (((Character.isDigit(s.charAt(index))) || (s.charAt(index) == &#39;.&#39;)));
    if (wasSeparator &amp;&amp; (!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 &lt; s.length; index++) {
    boolean isSeparator = (((Character.isDigit(s.charAt(index))) || (s.charAt(index) == &#39;.&#39;)));
    if (isSeparator) {
        if (start &gt;= 0) {
            end = index;
            elements[itemIndex++] = s.substring(start, end);
            start = -1;
            end = -1;
        }
    } else {
        if (start == -1) start = index;
    }
}
if (start &gt;= 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 = &quot;Nagapur82. Nanavade83. Nandur84. Nandurkichi Wadi85.&quot;;
        
        s = s.replace(&quot; &quot;, &quot;&quot;);
        s = s.replace(&quot;.&quot;, &quot;&quot;);
        String[] s2 = s.split(&quot;[0-9]+&quot;);

This may work

huangapple
  • 本文由 发表于 2020年4月5日 18:51:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61041421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定