0
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.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
  • It would help if you provided some sample data that demonstrates the issue. – John3136 Oct 22 '15 at 01:40
  • 2
    Possible duplicate: [http://stackoverflow.com/questions/16217627/string-split-at-a-meta-character](http://stackoverflow.com/questions/16217627/string-split-at-a-meta-character) – Slartibartfast Oct 22 '15 at 01:42

2 Answers2

2

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

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
  • @THOMASOCONNOR If this answered your question, you should click the checkmark to the left of the answer. That will increase my reputation, and it will increase your acceptance rate so others are more likely to answer your questions in the future. – Brett Kail Oct 23 '15 at 00:14
2

'+', '*', '?' 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.

Slartibartfast
  • 1,592
  • 4
  • 22
  • 33