syntax - Java Substring Missing Character? -


i'm having issue following code , i'm not sure start:

                system.out.println("subject: " + message.getsubject());                 subjectpack = message.getsubject().replaceall("([,|!|@|$|&|%|^|*|;|:|<|>|\"|\'|(|)|/|\\|+])", "").replaceall("[.](?![\\w]{2,4}$)", "").replaceall("( )+|(\t)", " ");                 system.out.println("subject line length: " + subjectpack.length());                 system.out.println("subject: " + subjectpack);                  subjectpack = subjectpack.touppercase().substring(message.getsubject().lastindexof("#")+1).touppercase();                 system.out.println("po length: " + subjectpack.length());                 system.out.println("po: " + subjectpack);               subjectpack = subjectpack.substring(0,6); 

and works fine several other examples except one.. why kicking "1"?

subject: fw: freight shifted on turbana load po#160753 subject line length: 44 subject: fw freight shifted on turbana load po#160753 po length: 5 po: 60753 exception in thread "main" java.lang.stringindexoutofboundsexception: string ind ex out of range: 6         @ java.lang.string.substring(unknown source)         @ picread.picturereader.sendmsg(picturereader.java:191)         @ picread.picturereader.main(picturereader.java:116) 

you extracting po subjectpack determine starting position message.getsubject()

    subjectpack.substring(message.getsubject().lastindexof("#")+1).touppercase(); //                        ^ determine starting position here //  ^ extracting string  // "fw: freight shifted on turbana load po#160753" // "fw freight shifted on turbana load po#160753" //  extracting ...            ^ position 

on side note: don't need call .touppercase() twice - either before or after .substring() operation sufficient.


Comments