StringIndexOutOfBoundException

后端 未结 2 380
攒了一身酷
攒了一身酷 2020-12-12 07:50

Hi I was just using stringbuilder to obtain certain part of the string using substring(int start, int end).. but I\'m having this error

08-17 20:27:40.737: E         


        
2条回答
  •  再見小時候
    2020-12-12 08:47

    Are you using the substring() method but you no has length lower than 4 ,so if you want to get only four number use the following code.

    Ex:

       class  SubString
          {
    public static void main(String[] args) 
    {
    StringBuilder str = new StringBuilder(phoneNumber);
        if(str.length()>4){
        System.out.println(str.substring(0,4));
        }
         }
        }
    

提交回复
热议问题