java - String starts with an empty String ("") -
my program reading text file , doing actions based on text. first line of text problematic. apparently starts "". messing startswith() checks.
to understand problem i've used code :
   system.out.println(thisline          + " -- first char : (" + thisline.charat(0)          + ") - starts ! : "          + thisline.startswith("!"));   string thisline first line in text file.
it writes console :  ! use ! add comments. lines starting ! not read. -- first char : () - starts ! : false
why happening , how fix this? want realize line starts "!" not ""
collecting mine , others' comments 1 answer posterity, string contains unprintable control characters. try
system.out.println( (int)thisline.charat(0) ) 
to print out numerical code or
my_string.replaceall("\\p{c}", "?"); 
to replace control characters '?'.
system.out.println( (int)thisline.charat(0) ) printed 65279 unicode code point zero-width space, not unprintable invisible on output. (see why  appearing in html?).
either remove whitespace character file, remove control characters string (my_string.replaceall("\\p{c}", "");) or use @arvind's answer , trim string (thisline = thisline.trim();) before reading contains no whitespace @ beginning or end of string.
edit: notepad won't show 'special' characters. if want edit file try hex editor or more advanced version of notepad such notepad++.
Comments
Post a Comment