-1
ArrayList<String> text = new ArrayList<String>(Arrays.asList(s.split("[ !,?._'@]+")));

Can anyone explain what this '+' sign after the square brackets in the split method used for?

  • It matches the expression from once to infinite, you can play with it here. https://regexr.com/61lug – Tiberiu C. Jul 11 '21 at 17:34
  • `split` is using *regular expressions* (regex) where `+` represents "one or more repetitions*. For more info see https://www.regular-expressions.info/repeat.html – Pshemo Jul 11 '21 at 17:50

2 Answers2

1

In regular expressions, + means "one or more". In your case, the split method will slice your string when it finds one or more occurences of the symbols inside the square brackets.

davidbuzatto
  • 9,207
  • 1
  • 43
  • 50
1

it is means "One or more" symbols. Example:

const str = '100dfsdfds00';
str.match(/[a-z]/g) // [d, f, s...]
str.match(/[a-z]+/g) // [fsdfds]

  • While the answer part is correct, the example won’t compile and is a very strange choice to highlight what `+` does in refer. – BeUndead Jul 11 '21 at 18:24