Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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 - Fatal编程技术网

如何从第二个空格中拆分java中的字符串?

如何从第二个空格中拆分java中的字符串?,java,Java,我有这个字符串: “上传已完成12345” 在java中: if (textforspeech.contains("upload completed")) { String[] split = textforspeech.split(" "); textforspeech = split[0

我有这个字符串:

“上传已完成12345”

在java中:

if (textforspeech.contains("upload completed"))
                                {
                                    String[] split = textforspeech.split(" ");
                                    textforspeech = split[0];
                                    status = (TextView) findViewById(R.id.progessStatus);
                                    status.setText(split[1]);
                                    MainActivity.this.initTTS();
                                }
问题是分割,从第一个空间分割。 所以我在变量textforspeech中得到的只是“upload”,在split[1]中得到的是completed12345

但我需要它以这种方式拆分:拆分[0]中的“上载完成”,拆分[1]中的仅为12345

        Pattern p = Pattern.compile("([\\D]*)([\\d]*)");
        Matcher m = p.matcher("upload completed12345");
        while (m.find()){
            System.out.println(m.group(1));
            System.out.println(m.group(2));
        }

如果您只关心
上传完成
,那么您可以这样做:

String completed = "Upload completed 12345";
for(String s : completed.split("\\s(?!(?=completed))")) {
    System.out.println(s);
}
基本上,它只是检查是否有空格后跟单词completed,并且不会在此空格上拆分。

import java.util.LinkedList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExExample {
    public static void main(String[] args) {
        String inputString = "I want To Split String By Space";
        // spaceIndexToSplitString= 1 -> [I , want To Split String By Space]
        // spaceIndexToSplitString= 2 -> [I want , To Split String By Space]
        // spaceIndexToSplitString= 3 -> [I want To , Split String By Space
        int spaceIndexToSplitString = 3;
        System.out.println(splitString(inputString, "\\s", spaceIndexToSplitString));
    }

    /**
     * As per input in method, iterate for occurrence of finding String(space).
     * In loop we are checking if we have exact count of occurrence, then take
     * stringSplitIndex of occurrence and break the loop. By getting
     * stringSplitIndex of occurrence, split string from 0 to stringSplitIndex
     * and add into the list and take another subString from stringSplitIndex to
     * end length of inputString.
     * 
     * @param inputString
     * @param regEx
     * @param occurancePosition
     * @return
     */
    public static List<String> splitString(String inputString, String regEx, int occurancePosition) {
        List<String> splitedStringList = new LinkedList<String>();
        // Check if inputString is not null
        if (inputString != null && inputString.length() > 0) {
            Pattern pattern = Pattern.compile(regEx);
            Matcher matcher = pattern.matcher(inputString);
            int count = 0;
            int stringSplitIndex = 0;
            // Iterate while loop for finding regEx
            while (matcher.find()) {
                // increase count if found the matcher string(regEx)
                count++;
                // check if we found expected occurrence of regEx in given
                // inputString
                if (count == occurancePosition) {
                    // assign index position of occurrence regEx.
                    stringSplitIndex = matcher.end();
                    break;
                }
            }
            // add subString of input String into the list from String index
            // position 0 to according to our occurrence position
            splitedStringList.add(inputString.substring(0, stringSplitIndex));
            // add SubString from occurrence index position to end of the
            // inputString.
            splitedStringList.add(inputString.substring(stringSplitIndex));
        }

        return splitedStringList;
    }
}
导入java.util.List; 导入java.util.regex.Matcher; 导入java.util.regex.Pattern; 公共类regexample{ 公共静态void main(字符串[]args){ String inputString=“我想按空格分割字符串”; //spaceIndexToSplitString=1->[我想按空格分割字符串] //spaceIndexToSplitString=2->[我想按空格分割字符串] //spaceIndexToSplitString=3->[我想,按空格分割字符串 int spaceIndexToSplitString=3; System.out.println(splitString(inputString,“\\s”,spaceIndexToSplitString)); } /**
*根据方法中的输入,迭代查找字符串(空格)。 *在循环中,我们检查是否有准确的发生次数,然后采取 *stringSplitIndex的出现和中断循环 *出现的stringSplitIndex,将字符串从0拆分为stringSplitIndex *并添加到列表中,并从stringSplitIndex获取另一个子字符串 *输入字符串的结束长度。 * *@param inputString *@param regEx *@参数发生位置 *@返回 */ 公共静态列表拆分字符串(字符串inputString、字符串regEx、int occurrenceposition){ List splitedStringList=新建LinkedList(); //检查inputString是否为空 if(inputString!=null&&inputString.length()>0){ Pattern=Pattern.compile(regEx); Matcher Matcher=pattern.Matcher(inputString); 整数计数=0; int stringSplitIndex=0; //迭代while循环以查找正则表达式 while(matcher.find()){ //如果找到匹配器字符串(regEx),则增加计数 计数++; //检查我们是否在给定的 //输入字符串 如果(计数==发生位置){ //分配引用正则表达式的索引位置。 stringSplitIndex=matcher.end(); 打破 } } //将输入字符串的子字符串从字符串索引添加到列表中 //根据我们的发生位置,位置0 添加(inputString.substring(0,stringSplitIndex)); //将子字符串从引用索引位置添加到 //输入字符串。 添加(inputString.substring(stringSplitIndex)); } 返回splitedStringList; } }
不要只是脱口而出代码。添加一些文本来解释代码如何回答问题。根据方法中的输入,迭代查找字符串(空格).在循环中,我们正在检查是否有精确的发生次数,然后获取发生次数的stringSplitIndex并中断循环。通过获取发生次数的stringSplitIndex,将字符串从0拆分为stringSplitIndex,并添加到列表中,然后从stringSplitIndex中获取另一个子字符串到inputString的结束长度。