Search code examples
javadelimiterstringtokenizer

How to spilt a sentence using words in StringTokenizer (JAVA)


I have a sentence, which has names of an authors separated by the word "and". I want to remove the "and" and put a & instead. Is there a quick and easy way to do this. I've tried scanner and useDelimiter(), StringTokenizer, and split.

This is for example what I want to split (I am getting this information from a file on my computer):

author={J. Park and J. N. James and Q. Li and Y. Xu and W. Huang}, 

So I used:

String author = nextLine.substring(nextLine.indexOf("{") + 1, nextLine.lastIndexOf("}"));

StringTokenizer st2 = new StringTokenizer(author, " and ");

while(st2.hasMoreTokens()){
      author += st2.nextToken() + " & ";
}

The output that I get is the following:

J. Park and J. N. James and Q. Li and Y. Xu and W. HuangJ. & P & rk & J. & N. & J & mes & Q. & Li & Y. & Xu & W. & Hu & g & .

I am not entirely sure what I am doing wrong. I googled this for 2 hours last night before giving up. I have tried using "[and]", "and", "^[and]$", but with no success.


Solution

  • Use the replaceAll method. For example:

    String s = "J. Park and J. N. James and Q. Li and Y. Xu and W. Huang";
    s = s.replaceAll("\\band\\b", "&"); // "J. Park & J. N. James & Q. Li & Y. Xu & W. Huang"
    

    The \b word boundaries ensures that if there is a name that includes and (for example, "Band") it won't be touched.