I want to know how to split strings in java, my API return data in the below formate days {moday, Tuesday,Wednesday,..} I want to split the string into words and save each word in a variable to perform desired actions on them I would be glad to get help from someone here.
Asked
Active
Viewed 259 times
0
-
Use the [String#split()](https://www.tutorialspoint.com/java/lang/string_split_limit.htm) method: `String[] weekDays = "Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday".split("\\s*,\\s*");`. The `\\s*` within the regular expression takes care of any spacing on either side of the comma delimiter (if any). – DevilsHnd - 退職した May 31 '22 at 20:42
-
thank you for your help, can you help me a little more with that how I should store the splited days in different variables – Shahzad Ali May 31 '22 at 20:53
-
1Does this answer your question? [Split string into individual words Java](https://stackoverflow.com/questions/11726023/split-string-into-individual-words-java). You can access the `String` array to obtain the different words and store them in variables. – Alias Cartellano May 31 '22 at 21:04
-
Just use the Array itself that the weekdays are split into. All you need to remember is that "Monday" (the first word in the delimited string) will always be at index 0 of the String[] Array. So, it you want to print/access "Tuesday" (the second word in the delimited string) you would use index 1: `System.out.println(weekDays[1]);`. Array indexing always start from 0. If this is not good enough then be more specific. – DevilsHnd - 退職した May 31 '22 at 21:40
-
actually, I'm using trying to use scheduling where API will return a string in which the user might have to save one or more days of the week and I want to split that string and compare it with the current system day if the string includes the current system day I want to print a message – Shahzad Ali Jun 01 '22 at 16:43