简体   繁体   中英

Interpreting the output of splitting with white spaces

How do we interpret the output of the flollowing execution

String s="   "  //3 white spaces
String arr[]=s.split(" ")   // output arr has no elements, same is the case on using \\s+ as the delimiter.

If we do the same in python, the op is ['' ,'' ,''] which is understood.

How does java split the above and why is there no element in arr ?

Per the documentation, "Trailing empty strings are therefore not included in the resulting array."

You may prefer to use s.split(" ", -1) if you want trailing empty strings.

Link: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM