This is a problem from the CodingBat website. I am pasting the problem first and discussing my efforts after that:
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"
This is what I wrote so far:
public String withoutString(String base, String remove) {
int len_b=base.length();
int len_r = remove.length();
String result="";
if(len_b<1 || len_r<1)
return "";
for (int i =0;i<=len_b-len_r;i++)
{
if(base.substring(i,i+len_r).equals(remove))
{
i=i+len_r-1;
}
else
{
result=result+base.substring(i,i+1);
}
}
if(!(base.substring(len_b-len_r+1, len_b).equals(remove)))
result=result+base.substring(len_b-len_r+1, len_b);
return result;
}
This passes all the test cases except for the ones where the removal of the string should be case-insensitive.
For example: withoutString("This is a FISH", "IS") → "Th a FH"
My code gives me "This is a FH" as I haven't handled case sensitivity in my code. I know that with Regex this could be done in one line. I am more interested in knowing if there is a way to handle these kinds of test cases in my present code.
Also, please let me know if my code could be made more efficient/elegant.
String has an equalsIgnoreCase(String s) method.
you can change this statement base.substring(i,i+len_r).equals(remove) to base.substring(i,i+len_r).equalsIgnoreCase(remove) using equalsIgnoreCase method.
hope helpful.
public String withoutString(String base, String remove)
{
String str=base;
String str1=remove;
String str3=str;
int k=str1.length();
for(int i=0;i<(str.length()-k+1);i++)
{
if(str1.equalsIgnoreCase(str.substring(i, i+k)))
{
String str4=str.substring(i, i+k);
str3=str3.replaceFirst(str4,"" );
}
}
return str3;
}
I did it without any looping :) I suppose it is not the best answer, but it works though
public String withoutString(String base, String remove) {
String lastString = base.replace(remove, "");
remove = remove.toLowerCase();
String veryLastString = lastString.replace(remove, "");
remove = remove.toUpperCase();
String veryVeryLastString = veryLastString.replace(remove, "");
return veryVeryLastString;
}
public String withoutString(String base, String remove) {
String b=base.toLowerCase();
String r=remove.toLowerCase();
if(b.length()<r.length()) return base;
if(b.contains(r)) b=b.replaceAll(r,"");
String temp="";
int j=0;
for(int i=0;i<base.length();i++)
if(j<b.length()){
if(base.substring(i,i+1).equalsIgnoreCase(b.substring(j,j+1))){
temp+=base.substring(i,i+1);
j++;
}
}
return temp;
}
Related
I am trying to write a recursive method in Java that accepts two strings and then goes ahead and removes the instances of the second string from the first string (one at a time).
ex. String 1 == Mississippi, String 2 iss
first recursion == Missippi
then the final result should return Mippi
public class RecursionEx {
public static void main(String[] args) {
String str1 = "Mississippi";
String str2 = "iss";
System.out.println(repString(str1, str2));
}
public static String repString(String string1, String string2) {
//base case
if(string1.length()== 0)
return "";
//recursive case
if (string1.substring(0, string2.length()) == string2)
return repString(string1.substring(string2.length()), string2);
else
return repString(string1.substring(1), string2);
}}
Like the comment suggests, you should use equals() when comparing strings in Java, but you can also simplify your life by using the contains and removeFirst method for strings to deal with this recursive task.
I added a print line to the recursive function to show it is removing one instance of string2 at a time from string1.
public class StringRecursion {
public static void main(String[] args) {
String str1 = "Mississippi";
String str2 = "iss";
System.out.println(repString(str1, str2));
}
public static String repString(String string1, String string2) {
if(string1.contains(string2)) {
string1 = string1.replaceFirst(string2, "");
System.out.println("The string is currently: "+ string1);
}
else {
return string1;
}
return repString(string1, string2);
}
}
Output:
The string is currently: Missippi
The string is currently: Mippi
Mippi
Important: One other thing to consider with such an approach is if you want the pattern "iss" formed by an intermediate removal to also be removed. For instance, if you have the word "iissss" and want to remove "iss" it would become "" after running this even though iss does not appear twice in the word initially.
If you want to have the behavior mimic replaceAll function, where we are looking to only get rid of the "iss" patterns in the very first word and not the ones that appear in intermediate steps, I believe the function:
public static String repString(String string1, String string2) {
if(string1.contains(string2)) {
Pattern pattern = Pattern.compile(string2);
long originalCounts = pattern.matcher(string1).results().count();
string1 = string1.replaceFirst(string2, "");
long newCounts = pattern.matcher(string1).results().count();
if(originalCounts == newCounts) {
Matcher matcher = pattern.matcher(string1);
matcher.find();
int startPosition = matcher.end();
//Skip the generated matching pattern that appears in-between.
return string1.substring(0, startPosition) + repString(string1.substring(startPosition), string2);
}
//System.out.println("The string is currently: "+ string1);
}
else {
return string1;
}
return repString(string1, string2);
}
will be sufficient instead.
So i'm using IntelliJ and the replace buzzword is highlighted. Most of the tips are over my head so i ignore them, but what i got for this one was that the result of string.replace is ignored. Why?
would i need something like ( string = string.replace(string.charAt(i));)?
import java.util.Scanner;
public class PhoneNumberDecipher {
public static String phoneNumber;
public static String Decipher(String string) {
string = phoneNumber;
for(int i =0; i<=phoneNumber.length(); i++) {
if(string.equalsIgnoreCase("A")
||string.equalsIgnoreCase("B")
||string.equalsIgnoreCase("C")) {
string.replace(string.charAt(i),'2')
}
else if(string.equalsIgnoreCase("D")
||string.equalsIgnoreCase("E")
||string.equalsIgnoreCase("F")) {
string.replace(string.charAt(i),'3');
}
else if(string.equalsIgnoreCase("G")
||string.equalsIgnoreCase("H")
||string.equalsIgnoreCase("I")) {
string.replace(string.charAt(i),'4');
}
else if(string.equalsIgnoreCase("J")
||string.equalsIgnoreCase("K")
||string.equalsIgnoreCase("L")) {
string.replace(string.charAt(i),'5');
}
else if(string.equalsIgnoreCase("M")
||string.equalsIgnoreCase("N")
||string.equalsIgnoreCase("O")) {
string.replace(string.charAt(i),'6');
}
else if(string.equalsIgnoreCase("P")
||string.equalsIgnoreCase("Q")
||string.equalsIgnoreCase("R")
|| string.equalsIgnoreCase("S")) {
string.replace(string.charAt(i),'7');
}
else if(string.equalsIgnoreCase("T")
||string.equalsIgnoreCase("U")
||string.equalsIgnoreCase("V")) {
string.replace(string.charAt(i),'8');
}
else if(string.equalsIgnoreCase("W")
||string.equalsIgnoreCase("X")
||string.equalsIgnoreCase("Y")
||string.equalsIgnoreCase("Z")) {
string.replace(string.charAt(i),'9');
}
}
return string;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please Enter a Phone Number you Wish to Decipher...");
phoneNumber = input.nextLine();
System.out.print(Decipher(phoneNumber));
}
}
String objects are immutable.
From the docs:
public String replace(char oldChar,char newChar)
Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
Hope this helps.
IntelliJ is complaining that you're calling a method whose only effect is to return a value (String.replace) but you're ignoring that value. The program isn't doing anything at the moment because you're throwing away all the work it does.
You need to use the return value.
There are other bugs in there too. You might be able to progress a little further if you use some of this code:
StringBuilder convertedPhoneNumber = new StringBuilder();
// Your loop begins here
char curCharacter = phoneNumber.charAt(i);
if (curCharacter == 'a') {
convertedPhoneNumber.append("2");
}
// More conditional logic and rest of loop goes here.
return convertedPhoneNumber.toString();
I had the same problem, but i did it like this:
String newWord = oldWord.replace(oldChar,newChar);
use that statement
string = string.replace(string.charAt(i));
Why? String is an immutable object. Look at this thread to get a complete explanation. This a fundemental part of Java, so make sure you learn it well.
string = string.replace(string.charAt(i), '<The char to replace>') will work.
Refer to this article you might understand better:
https://alvinalexander.com/blog/post/java/faq-why-isnt-replace-replaceall-replacefirst-not-working/
Since string is immutable, you have to reassign the a string with the string.replace() string.
Is there a command in java to remove the rest of the string after or before a certain word;
Example:
Remove substring before the word "taken"
before:
"I need this words removed taken please"
after:
"taken please"
String are immutable, you can however find the word and create a substring:
public static String removeTillWord(String input, String word) {
return input.substring(input.indexOf(word));
}
removeTillWord("I need this words removed taken please", "taken");
There is apache-commons-lang class StringUtils that contains exactly you want:
e.g. public static String substringBefore(String str, String separator)
public static String foo(String str, String remove) {
return str.substring(str.indexOf(remove));
}
Clean way to safely remove until a string
String input = "I need this words removed taken please";
String token = "taken";
String result = input.contains(token)
? token + StringUtils.substringAfter(string, token)
: input;
Apache StringUtils functions are null-, empty-, and no match- safe
Since OP provided clear requirements
Remove the rest of the string after or before a certain word
and nobody has fulfilled those yet, here is my approach to the problem. There are certain rules to the implementation, but overall it should satisfy OP's needs, if he or she comes to revisit the question.
public static String remove(String input, String separator, boolean before) {
Objects.requireNonNull(input);
Objects.requireNonNull(separator);
if (input.trim().equals(separator)) {
return separator;
}
if (separator.isEmpty() || input.trim().isEmpty()) {
return input;
}
String[] tokens = input.split(separator);
String target;
if (before) {
target = tokens[0];
} else {
target = tokens[1];
}
return input.replace(target, "");
}
I'm trying to write some code that will tell me if the first letter of one string equals the first letter of another. I can't figure out how to compare a string to the return of charAt(). Help?
public class CharAtTest
{
public static void main(String [] args)
{
String name = "joe";
String initial = "j";
if(initial.equals(name.charAt(0)))
{
System.out.println("Sucess");
}else{
System.out.println("Fail");
}
}
}
You could use:
if (initial.charAt(0) == name.charAt(0))
or better
if (name.startsWith(initial)) {
charAt(), like the name implies, returns a char, not a string. So make initial a char instead.
Simply do this:
String name = "joe";
char initial = 'j';
if (name.charAt(0) == initial) {
// ...
}
Another way would be:
String.valueOf(name.charAt(0));
Or:
Character.toString(name.charAt(0));
But the implicit way (see ivanovic's answer) works as well
name.charAt(0) + ""
How to insert a string enclosed with double quotes in the beginning of the StringBuilder and String?
Eg:
StringBuilder _sb = new StringBuilder("Sam");
I need to insert the string "Hello" to the beginning of "Sam" and O/p is "Hello Sam".
String _s = "Jam";
I need to insert the string "Hello" to the beginning of "Jam" and O/p is "Hello Jam".
How to achieve this?
The first case is done using the insert() method:
_sb.insert(0, "Hello ");
The latter case can be done using the overloaded + operator on Strings. This uses a StringBuilder behind the scenes:
String s2 = "Hello " + _s;
Other answers explain how to insert a string at the beginning of another String or StringBuilder (or StringBuffer).
However, strictly speaking, you cannot insert a string into the beginning of another one. Strings in Java are immutable1.
When you write:
String s = "Jam";
s = "Hello " + s;
you are actually causing a new String object to be created that is the concatenation of "Hello " and "Jam". You are not actually inserting characters into an existing String object at all.
1 - It is technically possible to use reflection to break abstraction on String objects and mutate them ... even though they are immutable by design. But it is a really bad idea to do this. Unless you know that a String object was created explicitly via new String(...) it could be shared, or it could share internal state with other String objects. Finally, the JVM spec clearly states that the behavior of code that uses reflection to change a final is undefined. Mutation of String objects is dangerous.
Sure, use StringBuilder.insert():
_sb.insert(0, _s);
You can add a string at the front of an already existing one. for example, if I have a name string name, I can add another string name2 by using:
name = name2 + name;
Don't know if this is helpful or not, but it works. No need to use a string builder.
private static void appendZeroAtStart() {
String strObj = "11";
int maxLegth = 5;
StringBuilder sb = new StringBuilder(strObj);
if (sb.length() <= maxLegth) {
while (sb.length() < maxLegth) {
sb.insert(0, '0');
}
} else {
System.out.println("error");
}
System.out.println("result: " + sb);
}
import java.lang.StringBuilder;
public class Program {
public static void main(String[] args) {
// Create a new StringBuilder.
StringBuilder builder = new StringBuilder();
// Loop and append values.
for (int i = 0; i < 5; i++) {
builder.append("abc ");
}
// Convert to string.
String result = builder.toString();
// Print result.
System.out.println(result);
}
}
It is better if you find quotation marks by using the indexof() method and then add a string behind that index.
string s="hai";
int s=s.indexof(""");