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(" ");
CodePudding user response:
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
CodePudding user response:
Lets say that we have this string : " Hello world " as you see we have sapce before Hello and space after world so trim will eliminate those spaces and split(' ') will make all word sparated by space in table like this ["Hello","world"]
CodePudding user response:
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.