可能重复:
How to check that the string contains the number format in Java?如何获取Java中两个数字之间的字符串?
可以说,我有以下格式的字符串:
string1 = "1. Stack Overflow 2. Google 3. Reddit"
如何挑选出来的数字之间的子?
可能重复:
How to check that the string contains the number format in Java?如何获取Java中两个数字之间的字符串?
可以说,我有以下格式的字符串:
string1 = "1. Stack Overflow 2. Google 3. Reddit"
如何挑选出来的数字之间的子?
遍历的String
每个字符,看其编号,然后马克开始索引和结束标记结束指数,做substring
* 方法使用*
请给出代码 – naresh
我们不是代码工厂。我在这个答案中给出了比代码:) –
您可以使用正则表达式和捕获组:
String str = "1. Stack overflow 2. Google";
Matcher m = Pattern.compile("\\d\\. (.*?) \\d").matcher(str);
if (m.find())
System.out.println(m.group(1)); // Prints "Stack overflow"
的正则表达式匹配上面的例子是
\d+\.\s(\D+)\d\.
转义为您提供方便。
这将捕获的数字(和点)之间的文本matchgroup的1
您可以用正则表达式做到这一点:
Pattern p = Pattern.compile("\\d+\\. (\\D+)\\d");
Matcher m = p.matcher(str);
if (m.find())
System.out.println(m.group(1));
或在一行中:str.replaceAll(“^。*?\\ d + \\。”,“”).replaceAll(“\\ d。*”,“”) – Ramon
我不会做你的功课。你没有付出任何努力。看看这个简单的方法。在你的代码中实现它并进行修改,以便它符合你的问题。
public boolean isInteger(String input)
{
try
{
Integer.parseInt(input);
return true;
}
catch(Exception)
{
return false;
}
}
字符串分割功能在这里工作也没关系:
String test = "1. Stack overflow 2. Google";
String[] test2 = test.split("\\d\\.");
test2[0]
将" Stack overflow"
test2[1]
将您可以使用test2[0].trim()
摆脱多余的空格。
-1显示绝对没有努力。 –
@MattBall我第二。 – 0xCAFEBABE
有些人不知道寻求帮助的正确方式,有些人在处理这个问题时有敌意。两种方法都同样无知。 – Bob