简体   繁体   中英

In Java what is a dangling meta character and how do I correct this error?

public class StringMerger {
public static boolean isMerge(String s, String part1, String part2) {
       for (int i = 0; i < part1.length(); i++)
           {
               s = s.replaceAll(part1.substring(i, i+1) , "");
           }
       for (int a =0; a < part2.length(); a++)
           {
               s = s.replaceAll(part2.substring(a , a+1) , "");
           }
       return (s.length() == 0);
 }
}

I am trying to see if the two Strings given part1 and part2 can combine to create the given String s .

This works for most regular cases.
However, under the category special cases I keep receiving an error:

Dangling meta character `+` near index 0

It is not always just + , sometimes it will * or ? .

What is a dangling meta character and is there any way for me to correct this in the given code.

The first argument to replaceAll is a regular expression. Use replace instead.

'+', '*', '?' and others are meta characters. replaceAll accepts regular expression as its argument. So when you try to use replaceAll , such characters would be treated as a part of regex. To make use of them, you will have to escape them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM