0

I have a String variable columnNames which includes the column names of a grid I am trying to sort. String columnNames = "To, From, RHVAC Technician Schedules Complete, MST Schedules Complete".

I am looking to split this into a String Array with 4 values. I have tried the following line of code :

String[] headersAllJobs = columnNames.split(" ");

However, when I run this, the commas are still stuck to the value so I have for example "To," and "From,". How can I split these values without the commas?

ScrapCode
  • 2,109
  • 5
  • 24
  • 44
jamiemca
  • 69
  • 9

3 Answers3

5
String columnNames = "To, From, RHVAC Technician Schedules Complete, MST Schedules Complete";
String[] headersAllJobs = columnNames.split(", ");
Prashant Zombade
  • 482
  • 3
  • 15
4

use the below code:

String[] split = columnNames.split(", ");

now
the value of split[0] will be To
split[1] will be From
split[2] will be RHVAC Technician Schedules Complete
...
...

TheSprinter
  • 1,523
  • 17
  • 30
2

According to JavaDocs, you can use split method to do it.

String columnNames = "To, From, RHVAC Technician Schedules Complete, MST Schedules Complete";
String[] headersAllJobs = columnNames.split(", ");
ScrapCode
  • 2,109
  • 5
  • 24
  • 44