1

I have following code in my program. It splits a line when a hyphen is encountered and stores each word in the String Array 'tokens'. But I want the hyphen also to be stored in the String Array 'tokens' when it is encountered in a sentence.

String[] tokens = line.split("-");

The above code splits the sentence but also totally ignores the hyphen in the resulting array. What can I do to store hyphen also in the resulting array?

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Suneeta Singh
  • 272
  • 3
  • 16

2 Answers2

1

Edit : -

Seems like you want to split on both whitespaces and hyphen but keeping only the hyphen in the array (As, I infer from your this line - stores each word in the String Array), you can use this: -

String[] tokens = "abc this is-a hyphen def".split("((?<=-)|(?=-))|\\s+");
System.out.println(Arrays.toString(tokens));

Output: -

[abc, this, is, -, a, hyphen, def]

For handling spaces before and after hyphen, you can first trim those spaces using replaceAll method, and then do split: -

"abc this is - a hyphen def".replaceAll("[ ]*-[ ]*", "-")
                            .split("((?<=-)|(?=-))|\\s+");

Previous answer : -

You can use this: -

String[] tokens = "abc-efg".split("((?<=-)|(?=-))");
System.out.println(Arrays.toString(tokens));

OUTPUT : -

[abc, -, efg]

It splits on an empty character before and after the hyphen (-).

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

I suggest to use a regular expression in combination with the Java Pattern and Matcher. Example:

String line = "a-b-c-d-e-f-";
Pattern p = Pattern.compile("[^-]+|-");
Matcher m = p.matcher(line);
while (m.find())
{
  String match = m.group();
  System.out.println("match:" + match);
}

To test your regular expression you could use an online regexp tester like this

Robe Elckers
  • 967
  • 1
  • 6
  • 19