Taking in two strings and returns a new string with the occurrences of the second string removed (No if statements)

remove characters from the first string which are present in the second string python
given two strings, find if first string is a subsequence of second
remove characters from the first string which are present in the second string leetcode
find the number of occurrences of a subsequence in a string
check if a string is a substring of another string in java
write a program to remove a given characters from string
substring in java
check if a string is a substring of another string in c

I'm not asking for an answer, I'm just looking for a hint. This is for a homework assignment and I have to write a method that takes in two strings and returns a new string, it has to look like the original string, but with occurrences of the second string removed. I can't use if statements.

public class Main {
    public static void main(String[]args) {
        System.out.println(without("operation", "ra"));
        System.out.println(without("checkers", "ers"));
        System.out.println(without("checkers", "ch"));
    }

    public static String without(String x, String y) {
        int indexNumb = x.lastIndexOf(y);
        int lastIndex = x.length();
        String firstWord = x.substring(0, indexNumb);
        String secondHalf = x.substring(indexNumb + 2, lastIndex);
        return firstWord + secondHalf;
    }
}

My output is:

opetion
checks
eckers

I'm supposed to get:

opetion
check
eckers

In the following line of code:

String secondHalf = x.substring(indexNumb + 2, lastIndex);

you make the assumption that y (i.e. the string to be removed) is two characters long. This assumption holds for your first and third testcase, but not for the second, which is why you get an extra letter in your output.

Use the following replacement instead:

String secondHalf = x.substring(indexNumb + y.length(), lastIndex);

Remove characters from the first string which are present in the , Remove characters from the first string which are present in the second string two strings as arguments and removes the characters from first string which are 3: Process each character of the input string and if count of that character is 0 then removed because 's' was present in mask_str but we we have got two extra� I'm not asking for an answer, I'm just looking for a hint. This is for a homework assignment and I have to write a method that takes in two strings and returns a new string, it has to look like the original string, but with occurrences of the second string removed. I can't use if statements.

The easiest way would be x.replaceAll(y, "");

In case you did not learn that command yet, or you are not allowed to use that command: Your solution is pretty good so far.

String secondHalf = x.substring(indexNumb + 2, lastIndex);

You are cutting two letters out of the String. This works fine for second strings like "ra" or "ch". To make it work for any second string, just replace the 2 by y.length().

String secondHalf = x.substring(indexNumb + y.length(), lastIndex);

Given two strings, find if first string is a subsequence of second , Given two strings str1 and str2, find if str1 is a subsequence of str2. Recursive C++ program to check if a string is subsequence of another string isSubSequence(str1, str2, m, n)? cout << "Yes " : cout << "No" ;. return 0;. } Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.� Given two strings, find if first string is a subsequence of second Given two strings str1 and str2, find if str1 is a subsequence of str2. A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements (source: wiki ).

I would do it using String .replace function. String replace function takes two arguments. First is the string pattern to be replaced and second what to replace it with. (Not submitting full answer as requested)

Comparing Strings and Portions of Strings (The Java™ Tutorials , Examples and practices described in this page don't take advantage of new features, enhancements, and removed or deprecated options for all JDK releases . The String class has a number of methods for comparing strings and portions of boolean startsWith(String prefix), Returns true if this string ends with or begins� Find and print the uncommon characters of the two given strings in sorted order. Here uncommon character means that either the character is present in one string or it is present in other string but not in both. The strings contain only lowercase characters and can contain duplicates.

Java Exercises: String exercises, 5. Write a java program to compare two strings lexicographically. Go to the editor. Sample Output: String 1: This is Exercise 1 String 2: This is� Rearrange characters in a string such that no two adjacent are same; Program to check if input is an integer or a string; Quick way to check if all the characters of a string are same; Program to find the initials of a name. Check Whether a number is Duck Number or not; Round the given number to nearest multiple of 10; Change string to a new

This should do it. You were setting a fixed length for the secondHalf, replacing it with y.length() fixes it.

public class Main {

public static void main(String[]args) {
    System.out.println(without("operation", "ra"));
    System.out.println(without("checkers", "ers"));
    System.out.println(without("checkers", "ch"));
}

public static String without(String x, String y) {
    int indexNumb = x.lastIndexOf(y);
    int lastIndex = x.length();
    String firstWord = x.substring(0, indexNumb);
    String secondHalf = x.substring(indexNumb + y.length(), lastIndex);
    return firstWord + secondHalf;
}

}

7. Strings — How to Think Like a Computer Scientist: Learning with , The len function returns the number of characters in a string: The expression fruit[-1] yields the last letter, fruit[-2] yields the second to last, if word == "banana ": print "Yes, we have no bananas! Instead of taking an index and extracting the corresponding character, it takes a This chapter introduced a lot of new ideas. m: Length of str1 (first string) n: Length of str2 (second string) If last characters of two strings are same, 1. We consider last characters and get count for remaining strings. So we recur for lengths m-1 and n-1. 2. We can ignore last character of first string and recurse for lengths m-1 and n.

8. Strings — How to Think Like a Computer Scientist: Learning with , upper is a method that can be invoked on any string object to create a new Note that indexing returns a string — Python has no special type for a single The len function, when applied to a string, returns the number of characters in a string: The expression fruit[-1] yields the last letter, fruit[-2] yields the second to last,� String is an array of characters. In this guide, we learn how to declare strings, how to work with strings in C programming and how to use the pre-defined string handling functions. We will see how to compare two strings, concatenate strings, copy one string to another & perform various string manipulation operations.

Java String, This Java String tutorial explains how to work with Java Strings. \n, Translated into a single new line character in the string If you want to be sure that two String variables point to separate These 3 consecutive quote characters tell the Java compiler that this is a for occurrences of the substring " is ". Returns a new character sequence constructed from beginIndex index up until endIndex - 1. String trim() Returns a copy of this string with leading and trailing white space removed. String toLowerCase() String toUpperCase() Returns a copy of this string converted to lowercase or uppercase.

Strings and string manipulation in C++, when we declare a string variable without initialization (as in the example above), In C++, we can use the + operator to concatenate (or “add”) two strings, like find, returns the position of the found character, or string::npos if no character is ) and taking 5 characters (up to and including the space after the a) is removed� Number of binary strings such that there is no substring of length ≥ 3; Print common characters of two Strings in alphabetical order; Meta Strings (Check if two strings can become same after a swap in one string) Python code to print common characters of two Strings in alphabetical order

Comments
  • something like x.replaceAll(y, ""); should work
  • We never learned that command so it wouldnt get credit
  • It's very hard for us to know what part of the String class you have or haven't learned.
  • 1) Split the string on the occurance of y into an array. 2) join all the parts together into a single string again
  • we learned Making a string, index, char, charAt, substring, lastIndexOf, length
  • This still fails, if y is not a part of x!
  • This fails, if y is not found. Furthermore, it removes only the last instance of y.
  • I think this is the best you can get without if statements.
  • In my answer, I pointed out some other options. Of course, all of them use some kind of condition. And this is also necessary for the corner cases I mentioned there.