简体   繁体   中英

java explain the String str.trim().split();

Can someone explain how this works? Which one works first and how can I use more than one method in one shot?

String[] temp = str.trim().split(" ");

Java always evaluating from left to right.

So, first you have a String ( str ). str.trim() returns a String that is trimmed. On that trimmed string that you now have, you can run another function, even when you didn't save it into a variable.

And then you run the split() method which returns an array of String s.

That depends on what do you have on your variable str.

Trim will remove all blanck spaces in the start and in the end of your string. Example: if you have a string like this " string " the result will be "string" .

Split will return an array of strings. example: if you have a string like this "this is my string" and you apply a split(" ") it will return to you something like this: ["this", "is", "my", "String"] .

Good coding

假设我们有这个字符串:“Hello world”,正如您所见,我们在 Hello 之前有 sapce,在 world 之后有空格,因此 trim 将消除这些空格,并且 split(' ') 将使表中的所有单词都被空格分隔,如下所示 ["Hello “,“世界”]

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