In: Computer Science
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2)
Must do 3 things:
1. Take two parameters, the original String (String s1) and the
substring (String s2) that is to be removed;
2. Create a new String that consists of the original String data
less all occurrences of the substring data;
3. Return the new String.
Note:
1. The data to be removed is each occurrence of the complete
substring, not individual characters of the substring. For example,
if the original String is “hello” and the substring is “el”, the
String to be returned is “hlo” and not “ho”.
2. The second, third and fourth examples include spaces, so the
substring also includes a space that is to be removed.
PLEASE HELP ME UNDERSTAND HOW TO COMPLETE THIS CODE BELOW:
public class RemoveSubstring
{
public static void main(String [] args)
{
String testString1 = "173167176531317";
String subStr1 = "17";
String testString2 = "I am not happy";
String subStr2 = "not ";
String testString3 = "I am not happy and not
glad";
String subStr3 = "not ";
String testString4 = "neither this nor that
works!";
String subStr4 = "neither this nor ";
System.out.println("The string " + testString1 + "
without " + subStr1 + " is: " + removeSubstring(testString1,
subStr1));
System.out.println("The string " + testString2 + "
without " + subStr2 + " is: " + removeSubstring(testString2,
subStr2));
System.out.println("The string " + testString3 + "
without " + subStr3 + " is: " + removeSubstring(testString3,
subStr3));
System.out.println("The string " + testString4 + "
without " + subStr4 + " is: " + removeSubstring(testString4,
subStr4));
}
public static String removeSubstring(String s1, String
s2)
{
return("PLEASE HELP ME UNDERSTAND HOW THIS WORKS OR WHERE TO LEARN
MORE I AM LOST HERE THANK YOU");
}
}
Function is thoroughly explained below in code comments. For your Better understanding i have also provided a screenshot of code in IDE.full program code is also given alongwith output screenshot. If need any further clarification please ask in comments.
#################################################################################
FUNCTION WITH EXPLANATION
public static String removeSubstring(String s1,String s2)
{
String result=""; //string to store our final result
for(int i=0;i<s1.length();i++) //loop through every character of string
{
if(s1.charAt(i)==s2.charAt(0)) //if the current character of string is equal to first character of substring
{
int j=0;
for(j=0;j<s2.length();j++) //loop through all characters of substring
{
if(s1.charAt(i+j)!=s2.charAt(j)) //if the character in string does not match with character in substring break the loop
break;
}
if(j==s2.length()) //if the j is equal to size of s2, that means there were all the characters of substring s2 in string s1
{
i=i+j-1; //increase the i until last element upto which substring is present in string
continue; //continue the loop because we dont want to store those charcters which match with substring
}
} //if condition closed
result=result+s1.charAt(i); //if the character does not match then add in result
} //for loop closed
return result;
}
SCREENSHOT IN IDE FOR BETTER UNDERSTANDING
##########################################################################
FULL CODE
public class RemoveSubstring
{
//method to remove all occurence of a substring from string
public static String removeSubstring(String s1,String s2)
{
String result=""; //string to store our final result
for(int i=0;i<s1.length();i++) //loop through every character of string
{
if(s1.charAt(i)==s2.charAt(0)) //if the current character of string is equal to first character of substring
{
int j=0;
for(j=0;j<s2.length();j++) //loop through all characters of substring
{
if(s1.charAt(i+j)!=s2.charAt(j)) //if the character in string does not match with character in substring break the loop
break;
}
if(j==s2.length()) //if the j is equal to size of s2, that means there were all the characters of substring s2 in string s1
{
i=i+j-1; //increase the i until last element upto which substring is present in string
continue; //continue the loop because we dont want to store those charcters which match with substring
}
} //if condition closed
result=result+s1.charAt(i); //if the character does not match then add in result
} //for loop closed
return result;
}
public static void main(String [] args)
{
String testString1 = "173167176531317";
String subStr1 = "17";
String testString2 = "I am not happy";
String subStr2 = "not ";
String testString3 = "I am not happy and not glad";
String subStr3 = "not ";
String testString4 = "neither this nor that works!";
String subStr4 = "neither this nor ";
System.out.println("The string " + testString1 + " without " + subStr1 + " is: " + removeSubstring(testString1, subStr1));
System.out.println("The string " + testString2 + " without " + subStr2 + " is: " + removeSubstring(testString2, subStr2));
System.out.println("The string " + testString3 + " without " + subStr3 + " is: " + removeSubstring(testString3, subStr3));
System.out.println("The string " + testString4 + " without " + subStr4 + " is: " + removeSubstring(testString4, subStr4));
}
}
##########################################################################
OUTPUT