How to remove last 3 characters from a list of String in java? -


i have list contains string in format 23:45:50 ,12:32:70 etc. want cut last 2 digits after :.

i using substring() not working properly,i posting code:

public class splitstring {  public static void main(string[] args) {      list<string> alist = new arraylist<string>();     alist.add("4:78:34");     alist.add("5:8:34");     alist.add("8:18:90");     alist.add("2:8:40");       for(int i=0;i<alist.size();i++){          string str = alist.get(i).substring(0, alist.get(i).length()-3);         alist.add(str);      }     system.out.println(alist); }  } 

but giving result

exception in thread "main" java.lang.stringindexoutofboundsexception: string index out of range: -2 @ java.lang.string.substring(unknown source) @ com.test.splitstring.main(splitstring.java:19) 

i want output 23:45,12:32

i think should create new list storage sub string. know when for-cycle break? debugged , break when alist.size()=0. exception causes when alist.get(i).length()<3. this:

`         list<string> alist = new arraylist<string>();         alist.add("4:78:34");         alist.add("5:8:34");         alist.add("8:18:90");         alist.add("2:8:40");         list<string> sublist = new arraylist<string>();         (int = 0; < alist.size(); i++)         {              string str = alist.get(i).substring(0, alist.get(i).length() - 3);             sublist.add(str);          }         system.out.println(sublist);` 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -