简体   繁体   中英

Whats wrong with this

It doesnt output anything. If i sound stupid im sorry im still a beginner

public class lololo{

     public static void main(String []args){
        String test = "mamara";
        String test2 = "ma";
        int pos = test.indexOf (test2);

        System.out.println(test.substring(0, pos) + test.substring(pos + test.length()));
     }
}

Try with this:

String test = "mamara";
String test2 = "ma";
int pos = test.indexOf(test2);

System.out.println("Position is: " + pos);

String res1 = test.substring(0, pos);
String res2 = test.substring(pos + test.length());

System.out.println("Result 1 is: " + res1);
System.out.println("Result 2 is: " + res2);
System.out.println("Final result is: " + res1 + res2);

The reason you do not see anything is the result equals an empty string. It means it is "".

Probably your logic of substring method calls is wrong:)

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