gudii9
asked on
substring example to delte string
Hi
I am trying below challenge
http://codingbat.com/prob/p100905
I wrote as below
public String delDel(String str) {
if((str.substring(1)).equa ls("del")) {
return str-"del";
}
return str;
}
what is the best way to see 'del' is there in given string and delete from exact same location. Please advise
I am trying below challenge
http://codingbat.com/prob/p100905
I wrote as below
public String delDel(String str) {
if((str.substring(1)).equa
return str-"del";
}
return str;
}
what is the best way to see 'del' is there in given string and delete from exact same location. Please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Using dpearson's and ozo's ideas, here is another solution.
public String delDel(String str) {
if(str.indexOf("del") == 1){
return str.replaceFirst("del","");
}
return str;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or, if you want to avoid calling StringBuilder unnecessarily:
return str.indexOf("del") == 1? new StringBuilder(str).delete( 1, 4).toString() : str;
return str.indexOf("del") == 1? new StringBuilder(str).delete(
He he - good one ;)
ASKER
if (pos == -1 || pos != 1)
why are we checking pos is not equal to 1 as above. Please advise
ASKER
return str.indexOf("del") == 1? new StringBuilder(str).delete(1, 4).toString() : str;
why are we checking indexOf("del") is 1 or not as above then if it then delete(1,4). Otherwise return same string
Please advise
ASKER
return ixDel == 1? sb.delete(ixDel, 4).toString() : str;
why we have given 4 as argument of delete method. I thought it should have been 3.
All the test cases passed as below
public String delDel(String str) {
StringBuilder sb = new StringBuilder(str);
int ixDel = sb.indexOf("del");
return ixDel == 1? sb.delete(ixDel, 4).toString() : str;
}
Expected Run
delDel("adelbc") → "abc" "abc" OK
delDel("adelHello") → "aHello" "aHello" OK
delDel("adedbc") → "adedbc" "adedbc" OK
delDel("abcdel") → "abcdel" "abcdel" OK
delDel("add") → "add" "add" OK
delDel("ad") → "ad" "ad" OK
delDel("a") → "a" "a" OK
delDel("") → "" "" OK
delDel("del") → "del" "del" OK
delDel("adel") → "a" "a" OK
delDel("aadelbb") → "aadelbb"
ASKER
str.replaceFirst("del","");
I was checking the API
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
I wonder why there is no replaceLast method similar to replaceFirst.
Please advise
if (pos == -1 || pos != 1)
why are we checking pos is not equal to 1 as above. Please advise
The problem said "if the string "del" appears starting at index 1" so checking if (pos != 1) is checking for whether the string is at index 1.
Looking at this I realize I made it more complex than it needs to be because of course if "pos == -1" then that means "pos != 1" is true, so we can just write this as:
if (pos != 1) ...
I wonder why there is no replaceLast method similar to replaceFirst.This is just because the people who wrote Java (Sun) thought that it would be more common for people to want to replace the first string that matches a pattern rather than the last string. When designing classes and methods there's a lot of asking "how will people most often want to use this" and that shows up in the design. They certainly could have added a "replaceLast" method. They just didn't think it would be used that often.
Doug
why are we checking indexOf("del") is 1 or not as above then if it then delete(1,4). Otherwise return same string
Please advise
Because that is what's specified
ASKER
return str.replaceFirst("(?<=^.)del","");
what is meaning of ?<=^.
Please advise
ASKER
I checked api
String replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement
It did not talk about ?<=^.
please advise
String replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement
It did not talk about ?<=^.
please advise
Did you see this part of the documentation?
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#sum
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#sum
ASKER
i need to read it more
ASKER
public String delDel(String str) {
StringBuilder sb = new StringBuilder(str);
int ixDel = sb.indexOf("del");
return ixDel == 1? sb.delete(ixDel, 4).toString() : str;
}
As above it is all working fine.
Below line
return ixDel == 1? sb.delete(ixDel, 4).toString() : str;
why we have given 4 as argument of delete method. I thought it should have been 3.
StringBuilder sb = new StringBuilder(str);
int ixDel = sb.indexOf("del");
return ixDel == 1? sb.delete(ixDel, 4).toString() : str;
}
As above it is all working fine.
Below line
return ixDel == 1? sb.delete(ixDel, 4).toString() : str;
why we have given 4 as argument of delete method. I thought it should have been 3.
Look at the API
http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#delete(int,%20int)
it says
end - The ending index, exclusive.
Therefore the end index is excluded from the delete.
So, if we want to delete "del" at index 1,2,3, then we should use 4 for end
http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#delete(int,%20int)
it says
end - The ending index, exclusive.
Therefore the end index is excluded from the delete.
So, if we want to delete "del" at index 1,2,3, then we should use 4 for end
Yeah - the key part as rrz mentioned is that the "end index" does NOT include that position.
This is true for all of the string operations.
So delete(0,1) means "delete the character starting at position 0 and ending BEFORE position 1" - so just 1 character at position 0.
etc.
Doug
This is true for all of the string operations.
So delete(0,1) means "delete the character starting at position 0 and ending BEFORE position 1" - so just 1 character at position 0.
etc.
Doug
ASKER
replace
public StringBuilder replace(int start,
int end,
String str)
Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. First the characters in the substring are removed and then the specified String is inserted at start. (This sequence will be lengthened to accommodate the specified String if necessary.)
Parameters:
start - The beginning index, inclusive.
end - The ending index, exclusive.
str - String that will replace previous contents.
Returns:
This object.
Throws:
StringIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.
I see replace function also has end exclusive.
What other functions of String has end as exclusive apart from delete, replace. please advise
Any string function that takes a range (e.g. substring) always has the end index being exclusive.
It would be very confusing if some of them weren't. They're all the same.
It would be very confusing if some of them weren't. They're all the same.
ASKER
StringBuilder delete(int start, int end)
Removes the characters in a substring of this sequence.
I see one other in StringBuilder with end exclusive
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
what is meaning of above code.
Is it is some kind of regular expression. Please advise