If I have a variable string which stores a different section of characters from a larger string, how can I split the larger string aroung the smaller string using the split command with regex?
For example the string variable may store “def”
The larger string may be “abcdefghi”
When the split command is preformed with the regex input as the string variable, the resulting split will be like this:
abc
ghi
edit: here is the code
String string = new String("2+22*5-1*2");
String multiply = new String("22*5");
int leftNum = Integer.parseInt(multiply.split("\\*")[0]);
int rightNum = Integer.parseInt(multiply.split("\\*")[1]);
System.out.println(string);
System.out.println(multiply);
System.out.println(leftNum * rightNum);
//the following should print "2+"
String left = string.split(multiply)[0];
System.out.println(left);
The \Q and \E escape out metacharacters for you.