有没有办法在不知道确切索引的情况下找出字符串末尾有多少个数字?
Is there a way to find out how many numbers are at the end of a string without knowing the exact index?
我有一个方法可以从字符串中提取特定的子字符串。该子字符串由字符串中的数字组成。然后将其解析为整数。
方法:
protected int startIndex() throws Exception {
String str = getWorkBook().getDefinedName("XYZ");
String sStr = str.substring(10,13);
return Integer.parseInt(sStr) - 1;
}
示例:
字符串:
'0 DB'!$B0
子字符串:
460
好吧,我手动输入了子字符串的索引范围。但我想自动化它。
我的做法:
String str = getWorkBook().getDefinedName("XYZ");
int length = str.length();
String sStr = str.substring(length - 3, length);
这对这个例子很有效。
现在出现的问题是字符串末尾的数字也可以是4位或5位。既然如此,我自然得了一个NullPointerException
.
有没有办法或其他方法找出字符串末尾有多少个数字?
在你的情况下,我建议像这样将正则表达式与 replaceAll 一起使用:
String sStr = str.replaceAll(".*?([0-9]+)$", "");
这将提取最后的所有数字或您的字符串或任何长度。
此外,我认为您忽略了字符串中没有数字的情况,为此我建议您在将字符串转换为整数之前检查您的字符串。
String sStr = str.replaceAll(".*?([0-9]+)$", "");
if (!sStr.isEmpty()) {
return Integer.parseInt(sStr) - 1;
}
return 0; // or any default value
您可以使用正则表达式,(?<=\D)\d+$
表示字符串末尾的一位或多位数字(即 \d+
),preceded by 非数字(即 \D
).
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(getNumber("'0 DB'!$B0"));
}
static String getNumber(String str) {
Matcher matcher = Pattern.compile("(?<=\D)\d+$").matcher(str);
if (matcher.find()) {
return matcher.group();
}
// If no match is found, return the string itself
return str;
}
}
如果您只想获取最后一个数字,可以在还原时遍历整个字符串并获取起始索引:
protected static int startIndex() {
String str = getWorkBook().getDefinedName("XYZ");
if(Character.isDigit(str.charAt(str.length() - 1))) {
for(int i = str.length() - 1; i >= 0; i--){
if(!Character.isDigit(str.charAt(i)))
return i+1;
}
}
return -1;
}
然后打印出来:
public static void main(String[] args) {
int start = startIndex();
if(start != -1)
System.out.println(getWorkBook().getDefinedName("XYZ").substring(start));
else
System.out.println("No Number found");
}
您必须添加
没有 RegEx 的简单快速的解决方案:
public class Main
{
public static int getLastNumber(String str) {
int index = str.length() - 1;
while (index > 0 && Character.isDigit(str.charAt(index)))
index--;
return Integer.parseInt(str.substring(index + 1));
}
public static void main(String[] args) {
final String text = "'0 DB'!$B0";
System.out.println(getLastNumber(text));
}
}
输出将是:
460
如果我要这样做,我会从末尾开始搜索。这是相当有效的。如果没有找到正数,则为 returns -1。也可以使用其他 return 选项和 OptionalInt
的使用。
String s = "'0 DB'!$B0";
int i;
for (i = s.length(); i > 0 && Character.isDigit(s.charAt(i-1)); i--);
int vv = (i < s.length()) ? Integer.valueOf(s.substring(i)) : -1;
System.out.println(vv);
版画
460
如果你知道最后总会有一个数字,你可以忘记上面的三进制 (?:),只需执行以下操作:
int vv = Integer.valueOf(s.substring(i));
我有一个方法可以从字符串中提取特定的子字符串。该子字符串由字符串中的数字组成。然后将其解析为整数。
方法:
protected int startIndex() throws Exception {
String str = getWorkBook().getDefinedName("XYZ");
String sStr = str.substring(10,13);
return Integer.parseInt(sStr) - 1;
}
示例:
字符串:
'0 DB'!$B0
子字符串:
460
好吧,我手动输入了子字符串的索引范围。但我想自动化它。
我的做法:
String str = getWorkBook().getDefinedName("XYZ");
int length = str.length();
String sStr = str.substring(length - 3, length);
这对这个例子很有效。
现在出现的问题是字符串末尾的数字也可以是4位或5位。既然如此,我自然得了一个NullPointerException
.
有没有办法或其他方法找出字符串末尾有多少个数字?
在你的情况下,我建议像这样将正则表达式与 replaceAll 一起使用:
String sStr = str.replaceAll(".*?([0-9]+)$", "");
这将提取最后的所有数字或您的字符串或任何长度。
此外,我认为您忽略了字符串中没有数字的情况,为此我建议您在将字符串转换为整数之前检查您的字符串。
String sStr = str.replaceAll(".*?([0-9]+)$", "");
if (!sStr.isEmpty()) {
return Integer.parseInt(sStr) - 1;
}
return 0; // or any default value
您可以使用正则表达式,(?<=\D)\d+$
表示字符串末尾的一位或多位数字(即 \d+
),preceded by 非数字(即 \D
).
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(getNumber("'0 DB'!$B0"));
}
static String getNumber(String str) {
Matcher matcher = Pattern.compile("(?<=\D)\d+$").matcher(str);
if (matcher.find()) {
return matcher.group();
}
// If no match is found, return the string itself
return str;
}
}
如果您只想获取最后一个数字,可以在还原时遍历整个字符串并获取起始索引:
protected static int startIndex() {
String str = getWorkBook().getDefinedName("XYZ");
if(Character.isDigit(str.charAt(str.length() - 1))) {
for(int i = str.length() - 1; i >= 0; i--){
if(!Character.isDigit(str.charAt(i)))
return i+1;
}
}
return -1;
}
然后打印出来:
public static void main(String[] args) {
int start = startIndex();
if(start != -1)
System.out.println(getWorkBook().getDefinedName("XYZ").substring(start));
else
System.out.println("No Number found");
}
您必须添加
没有 RegEx 的简单快速的解决方案:
public class Main
{
public static int getLastNumber(String str) {
int index = str.length() - 1;
while (index > 0 && Character.isDigit(str.charAt(index)))
index--;
return Integer.parseInt(str.substring(index + 1));
}
public static void main(String[] args) {
final String text = "'0 DB'!$B0";
System.out.println(getLastNumber(text));
}
}
输出将是:
460
如果我要这样做,我会从末尾开始搜索。这是相当有效的。如果没有找到正数,则为 returns -1。也可以使用其他 return 选项和 OptionalInt
的使用。
String s = "'0 DB'!$B0";
int i;
for (i = s.length(); i > 0 && Character.isDigit(s.charAt(i-1)); i--);
int vv = (i < s.length()) ? Integer.valueOf(s.substring(i)) : -1;
System.out.println(vv);
版画
460
如果你知道最后总会有一个数字,你可以忘记上面的三进制 (?:),只需执行以下操作:
int vv = Integer.valueOf(s.substring(i));