I want to make this text below
String text = " good morning \t\thow \n\nare you today ";
become
[good, morning, how, are, you, today]
I have tried to split it using .split(["\\s+"]) but it still count the whitespace at the start of the text and store it to array.
String text = " good morning \t\thow \n\nare you today ";
String[] items = text.split("\\s+");
System.out.println("Items = " + Arrays.toString(items));
This is the result I get
Items = [, good, morning, how, are, you, today]
I have tried using items.remove(0) but the first index still can’t be removed. How to erase the first index?
>Solution :
Use trim() to remove the leading and trailing whitespace before splitting the string.
String text = " good morning \t\thow \n\nare you today ";
String[] items = text.trim().split("\\s+");
System.out.println("Items = " + Arrays.toString(items));