public String plusOut(String str, String word) {
return str.replaceAll(".word.","+word+");
}
I am getting below resultExpected Run
plusOut("12xy34", "xy") → "++xy++" "12xy34" X
plusOut("12xy34", "1") → "1+++++" "12xy34" X
plusOut("12xy34xyabcxy", "xy") → "++xy++xy+++xy" "12xy34xyabcxy" X
plusOut("abXYabcXYZ", "ab") → "ab++ab++++" "abXYabcXYZ" X
plusOut("abXYabcXYZ", "abc") → "++++abc+++" "abXYabcXYZ" X
plusOut("abXYabcXYZ", "XY") → "++XY+++XY+" "abXYabcXYZ" X
plusOut("abXYxyzXYZ", "XYZ") → "+++++++XYZ" "abXYxyzXYZ" X
plusOut("--++ab", "++") → "++++++" "--++ab" X
plusOut("aaxxxxbb", "xx") → "++xxxx++" "aaxxxxbb" X
plusOut("123123", "3") → "++3++3" "123123" X
other tests
for (int y=0;y<str.length();y++){
if(!(str.indexOf(word,y)==y)){
str=str.replace(str.charAt(y),'+');
}
else{y+= (word.length()-1);}
}
return str;
public String plusOut(String str, String word) {
StringBuilder sb = new StringBuilder(str);
int y = 0;
while (y < sb.length()) {
if (!(sb.indexOf(word, y) == y)) {
sb.setCharAt(y, '+');
y++;
} else {
y += word.length();
}
}
return sb.toString();
}
public String plusOut(String str, String word) {
for ( int y=0;y<str.length();y++){
if(!(str.indexOf(word,y)==y)){
char[] ch = str.toCharArray();
ch[y]='+';
str = new String(ch);
}
else{y+= (word.length()-1);}
}
return str;
}
ASKER
public class PlusOutEx {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("value is"+plusOut("12xy34", "xy")); //→ "++xy++"
}
public static String plusOut(String str, String word) {
int x=str.indexOf(word);
System.out.println("x is--->"+x);
System.out.println("--->"+str.substring(x,x+2));
return str.replaceAll(str.substring(x,x+2),"++");
}
}
i wrote as above but that needs to be refined to get opposite effect
ASKER
ASKER
ASKER
public String plusOut(String str, String word) {
String out="";
String w="";
for( String p:str.split("\\Q"+word,-1) ){
out += w;
out += p.replaceAll(".","+");
w = word;
}
return out;
}
ASKER
for( String p:str.split("\\Q"+word,-1)){
ASKER
if (!(str.indexOf(str.charAt(i)) == idx)) {
public class PlusOutEx {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("value is"+plusOut("12xy34", "xy")); //→ "++xy++"
}
public static String plusOut(String str, String word) {
StringBuilder sb = new StringBuilder();
int len = word.length();
for (int i = 0; i < str.length(); i++) {
int idx = str.indexOf(word);
if (!(str.indexOf(str.charAt(i)) == idx)) {
sb.append("+");
} else {
sb.append(word);
i+= (len - 1);
}
}
return sb.toString();
}
}
ASKER
ASKER
i+= (len - 1);
ASKER
public String plusOut(String str, String word) {
for (int y=0;y<str.length();y++){
if(!(str.indexOf(word,y)==y)){
str=str.replace(str.charAt(y),'+');
}
else{y+= (word.length()-1);}
}
return str;
}
Expected Run
plusOut("12xy34", "xy") → "++xy++" "++xy++" OK
plusOut("12xy34", "1") → "1+++++" "1+++++" OK
plusOut("12xy34xyabcxy", "xy") → "++xy++xy+++xy" "++xy++xy+++xy" OK
plusOut("abXYabcXYZ", "ab") → "ab++ab++++" "ab++ab++++" OK
plusOut("abXYabcXYZ", "abc") → "++++abc+++" "++++++++++" X
plusOut("abXYabcXYZ", "XY") → "++XY+++XY+" "++XY+++XY+" OK
plusOut("abXYxyzXYZ", "XYZ") → "+++++++XYZ" "++++++++++" X
plusOut("--++ab", "++") → "++++++" "++++++" OK
plusOut("aaxxxxbb", "xx") → "++xxxx++" "++xxxx++" OK
plusOut("123123", "3") → "++3++3" "++3++3" OK
other tests
OK
ASKER
if(!(str.indexOf(word,y)==y)){
ASKER
str.replace(str.charAt(y),'+'); may replace more than one character
ASKER
str.replace(str.charAt(y),'+'); may replace more than one character
public String plusOut(String str, String word) {
for (int y=0;y<str.length();y++){
if(!(str.indexOf(word,y)==y)){
str=str.replace(str.charAt(y),'+');
}
else{y+= (word.length()-1);}
}
return str;
}
public String plusOut(String str, String word) {
for (int y=0;y<str.length();y++){
if(!(str.indexOf(word,y)==y)){
str=str.replaceFirst("\\Q"+str.charAt(y),"+");
}
else{y+= (word.length()-1);}
}
return str;
}
But that would still fail if one of the tests was plusOut("xyx", "xy")
public String plusOut(String str, String word) {
for (int y=0;y<str.length();y++){
if(!(str.indexOf(word,y)==y)){
str=str.replaceFirst("(.{"+y+"}).","$1+");
}
else{y+= (word.length()-1);}
}
return str;
}
ASKER
public String plusOut(String str, String word) {
for (int y=0;y<str.length();y++){
if(!(str.indexOf(word,y)==y)){
str=str.replaceFirst("(.{"+y+"}).","$1+");
}
else{y+= (word.length()-1);}
}
return str;
}
ASKER
ASKER
ASKER
how you remember all above methodI don't.
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY
Basically you avoided the problem "you cannot negate a whole word in regex" by using NUL instead of the search string.