简体   繁体   中英

Java split line not working

this is my code

//Numbers (Need errors on sort and numbers)
if(line.contains("n"))
{
    //split the line with space
    String[] LineSplit = line.split(" ");

    if(LineSplit[0].contains("n"))
    {
        //split the already split line  with "n thename "
        String[] LineSplit2 = line.split("n " + LineSplit[0] + " ");

        String text = "var " + LineSplit[1] + "=" + LineSplit2[0] +  ";";
        text = text.replace("\n", "").replace("\r", "");

        JAVASCRIPTTextToWrite += text;
    }

}

the line of text is n number 1

the output should be

var number = 1;

but the output is

var number=n number = 1;

can some one please tell me how to fix this? the code looks right but doesn't work :(

String number = "n number 1";
Sting[] temp = number.split(" ");
StringBuilder sb = new StringBuilder("var ");
sb.append(temp[1]);
sb.append(temp[2]);

perform this operation if your condition satisfied

            String line = "n number 1";
            String JAVASCRIPTTextToWrite="";
            if(line.contains("n"))
            {
                //split the line with space
                String[] LineSplit = line.split(" ");

                if(LineSplit[0].contains("n"))
                {
                    //split the already split line  with "n thename "
                    String[] LineSplit2 = line.split("n " + LineSplit[1] + " ");
                    System.out.println( LineSplit[1]);
                    System.out.println( LineSplit2[0]);
                    String text = "var " + LineSplit[1] + "=" + LineSplit2[1] +  ";";
                    text = text.replace("\n", "").replace("\r", "");

                    JAVASCRIPTTextToWrite += text;
                    System.out.println(JAVASCRIPTTextToWrite);
                }

            }
String line = "n number 1";

   if(line.contains("n"))
     {
    //split the line with space
    String[] LineSplit = line.split(" ");
        if(LineSplit[0].contains("n")) {
        //split the already split line  with "n thename "
        String LineSplit2 = line.substring(line.lastIndexOf(" ") + 1 , line.length());

        String text = "var " + LineSplit[1] + "=" + LineSplit2 +  ";";
        //text = text.replace("\n", "").replace("\r", "");
        System.out.println(text);
        }

   }

Output:

var number=1;

I do not know what is your purpose for split the string twice. Just for the out put you want, I think the solution below is enough. Please look at the code below whether is you want:

    String line = "n number 1";
    String JAVASCRIPTTextToWrite = "";
    //Numbers (Need errors on sort and numbers)
    if(line.contains("n")) {
        //split the line with space
        String[] LineSplit = line.split(" ");

        if(LineSplit.length == 3) {
            StringBuilder text = new StringBuilder();
            text.append("var ");
            text.append(LineSplit[1]);
            text.append("=");
            text.append(LineSplit[2]);
            text.append(";");

            JAVASCRIPTTextToWrite += text.toString().replace("\n", "").replace("\r", "");
            System.out.println(JAVASCRIPTTextToWrite);
        }
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM