Friday, April 7, 2017

[Java][Exerise] Replace string with non case sensitive.

 Question:

Given two strings, base and remove, return a version of the base string where all instances of the remove string have been removed (not case sensitive). You may assume that the remove string is length 1 or more. Remove only non-overlapping instances, so with "xxx" removing "xx" leaves "x".

withoutString("Hello there", "llo") → "He there"
withoutString("Hello there", "e") → "Hllo thr"
withoutString("Hello there", "x") → "Hello there"
 

Expected result

Solution

public String withoutString(String base, String remove) {
  return base.replaceAll("(?i)"+remove,"");
}

Reference

https://stackoverflow.com/questions/13714549/java-string-split-on-non-alphabetic-characters#13714725
http://codingbat.com/prob/p192570

No comments :

Post a Comment