Skip to main content
Code Review

Return to Question

typo in title, and formatting.
Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419

Replacing all occrancesoccurrences of a substring in a string without using regex

Problem Statement: 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".

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"

Please feel free to review the code below:

public String withoutString(String base, String remove) {
 final int rLen = remove.length();
 final int bLen = base.length();
 String op = "";
 
 for(int i = 0; i < bLen;)
 {
 if( !(i + rLen > bLen) && base.substring(i, i + rLen).equalsIgnoreCase(remove) )
 {
 i += rLen;
 continue;
 }
 op += base.substring(i, i + 1);
 i++;
 }
 
 return op; 
 
}

Replacing all occrances of a substring in a string without using regex

Problem Statement: 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"

Please feel free to review the code below:

public String withoutString(String base, String remove) {
 final int rLen = remove.length();
 final int bLen = base.length();
 String op = "";
 
 for(int i = 0; i < bLen;)
 {
 if( !(i + rLen > bLen) && base.substring(i, i + rLen).equalsIgnoreCase(remove) )
 {
 i += rLen;
 continue;
 }
 op += base.substring(i, i + 1);
 i++;
 }
 
 return op; 
 
}

Replacing all occurrences of a substring in a string without using regex

Problem Statement:

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"

Please feel free to review the code below:

public String withoutString(String base, String remove) {
 final int rLen = remove.length();
 final int bLen = base.length();
 String op = "";
 
 for(int i = 0; i < bLen;)
 {
 if( !(i + rLen > bLen) && base.substring(i, i + rLen).equalsIgnoreCase(remove) )
 {
 i += rLen;
 continue;
 }
 op += base.substring(i, i + 1);
 i++;
 }
 
 return op; 
 
}
Source Link
Anirudh
  • 872
  • 2
  • 13
  • 28

Replacing all occrances of a substring in a string without using regex

Problem Statement: 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"

Please feel free to review the code below:

public String withoutString(String base, String remove) {
 final int rLen = remove.length();
 final int bLen = base.length();
 String op = "";
 
 for(int i = 0; i < bLen;)
 {
 if( !(i + rLen > bLen) && base.substring(i, i + rLen).equalsIgnoreCase(remove) )
 {
 i += rLen;
 continue;
 }
 op += base.substring(i, i + 1);
 i++;
 }
 
 return op; 
 
}
lang-java

AltStyle によって変換されたページ (->オリジナル) /