Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
串联Java字符串中的连续整数_Java_String_Parsing_Char - Fatal编程技术网

串联Java字符串中的连续整数

串联Java字符串中的连续整数,java,string,parsing,char,Java,String,Parsing,Char,我正在尝试查找以下Java代码: 1) 检查字符串是否包含任何连续整数 2) 如果它包含连续的整数,则将它们串联起来,即当前消息中的整数仅用空格分隔,因此我需要一种将这些空格分隔的整数串联起来的方法。例如: message1 = "My no is 9543 21 00 10" desired output = "My no is 9543210010" message2 = "You can reach 2 me at 42315 468" desired output = "You can

我正在尝试查找以下Java代码:

1) 检查字符串是否包含任何连续整数

2) 如果它包含连续的整数,则将它们串联起来,即当前消息中的整数仅用空格分隔,因此我需要一种将这些空格分隔的整数串联起来的方法。例如:

message1 = "My no is 9543 21 00 10"
desired output = "My no is 9543210010"

message2 = "You can reach 2 me at 42315 468"
desired output = "You can reach 2 me at 42315468"
我的主要问题是解决电话号码的上述问题,即将空格分隔的电话号码连接在一起,因此如果有人有不同的方法来解决这个问题,请让我知道

我尝试了以下代码,但当字符串以空格结尾时它不起作用:

if(message.matches(".*\\d.*")){             
    for (int i = 0; i <= message.length() -2 ; ++i){             
        if ((Character.isDigit(message.charAt(i))) && message.charAt(i+1) == ' ' && (Character.isDigit(message.charAt(i+2))))  {                        
            StringBuilder sb = new StringBuilder(message);
            sb.deleteCharAt(i+1);
            message = sb.toString();
        }
    }                       
}
if(message.matches(.*\\d.*){

对于(inti=0;i这是未经测试的代码,只是用记事本写的,但我想你会明白要点的

基本上,将字符串拆分为一个以空格(“”)为分隔符的数组,将所有内容粘合在一起。如果索引+1不是数字,请添加空格,否则不要添加空格

我希望这有帮助

String msg = "abc 1 2 3 333 yoo"
String[] splitted = msg.Split(' ');
String output = "";

for(int i = 0; i < splitted.Length - 1; i++) {

    output += splitted[i];
    if(i + 1 < splitted.Length - 1) {
        if(isInteger(splitted[i]) && isInteger(splitted[i+1]))
            continue;   
    }
    output += " " 
}
output = output.Trim();

public static boolean isInteger(String s) {
    try { 
        Integer.parseInt(s); 
    } catch(NumberFormatException e) { 
        return false; 
    } catch(NullPointerException e) {
        return false;
    }
    // only got here if we didn't return false
    return true;
}
String msg=“abc 12333 yoo”
String[]splitted=msg.Split(“”);
字符串输出=”;
for(int i=0;i
基于peshmo评论的社区维基答案


换句话说,您正在尝试删除由数字包围的空格

message = message.replaceAll("(?<=\\d)\\s+(?=\\d)","");

message=message.replaceAll(“(?)换句话说,您正在尝试删除由数字包围的空格。请尝试使用
message=message.replaceAll(”(?@Pshemo,应该作为答案发布。@TimB根据问题描述,我怀疑提供的示例可能过于简单,这就是为什么我只发布评论而不是答案(如果我怀疑答案需要更正,我不喜欢发布,因为有人没有提供所有必要的信息)。我希望OP能以一些实际案例做出回应,而这个解决方案在这些案例上是行不通的,那么我们就可以开始思考真正的答案了。但希望我错了:)你的解决方案对我有效@Pshemo@InheritedGeek然后,请随意回答Tim发布的问题。如果这段过于冗长的代码是出于教育目的,那么您应该注意其中的几点:*不要忘记;在这行的末尾(好的,您声明它未经测试)*不要依赖于正常程序流的异常*不要在循环中连接字符串-改用StringBuilder。这只是一个概念证明;只是为了抓住问题的关键点。这会起作用,但有更简单的方法。例如,请参阅我刚刚发布的CW答案。如果你是编程新手(我从问题中暗示)与立即开始学习正则表达式相比,将事情分解并将它们粘在一起更容易理解。