java - Why does System.out.println print a new line while System.out.print prints nothing? -
i having trouble split function. not know how works.
here source code:
// using println print out result string str = " welcome java tutorial "; str = str.trim(); string[] arr = str.split(" "); (string c : arr) { system.out.println(c); } //using print print out result string str = " welcome java tutorial "; str = str.trim(); string[] arr = str.split(" "); (string c : arr) { system.out.print(c); }
the first result when using println
, second result when using print
,
i not understand why space appears in println
, while not appear in print
. can explain me?
since have many spaces in string, if @ output of split function, resulted array looks
[welcome, , , , , , , to, , , , java, , , , , tutorial]
so if close empty string's ""
.
when println("")
printing line no string.
however when print("")
, no more visibility of string , looks nothing getting printed.
if want separate them regardless of spaces between them, split them white space.
lastly, trim() won't remove spaces within string. can trim spaces in first , last.
Comments
Post a Comment