views:

1171

answers:

6

If you are using Java or JavaScript, is there a good way to do something like a String subtraction so that given two strings:

org.company.project.component
org.company.project.component.sub_component

you just get

sub_component

I know that I could just write code to walk the string comparing characters, but I was hoping there was a way to do it in really compact way.

EDIT TO ADD: As I was analyzing the data I get when I use this I realized that I missed the statement of the case a bit. It should be:

org.company.project.component.diff
org.company.project.component.sub_component

So, I actually only want to remove the sections that are identical. That said, the answer that I accepted is closest to my goal.

+3  A: 
String result = "org.company.project.component.sub_component".replace("org.company.project.component","")

Should work...

EDIT: Apache commons libraries are also great to use

As noted below, the StringUtils class does in fact have a method for this: StringUtils.remove()

Chris Marasti-Georg
+1  A: 

Can't you just replace the occurrences of the first string in the second with an empty string ?

axk
+4  A: 

Depends on precisely what you want. If you're looking for a way to compare strings in the general case -- meaning finding common sub-strings between arbitrary inputs -- then you're looking at something closer to the Levenshtein distance and similar algorithms. However, if all you need is prefix/suffix comparison, this should work:

public static String sub(String a, String b) {
    if (b.startsWith(a)) {
        return b.subString(a.length());
    }

    if (b.endsWith(a)) {
        return b.subString(0, b.length() - a.length());
    }

    return "";
}

...or something roughly to that effect.

Daniel Spiewak
Thanks Chris and Daniel (and all the rest of you). That was amazingly fast.
Scott Rosenbaum
This seems like way too much code for what's requested - Erickson's approach (replace string B in string A with empty string) is much simpler and readable.
Herb Caudill
@Herb Agreed, I do like Erickson's (and Chris's) approach much better for readability. However, his doesn't enforce strict prefix/suffix. The problem I have is the question is a bit ill-defined: what does String difference really mean?
Daniel Spiewak
A: 

If you're just trying to get whatever's after the last dot, I find this method easy in Javascript:

var baseString = "org.company.project.component.sub_component";
var splitString = baseString.split(".");
var subString = splitString[splitString.length - 1];

subString will contain the value you're looking for.

Joshua Carmody
A: 

At first glance, I thought of RegExp, but adding to the question, you removed that possibility by adding to the start-string ...

So you'll have to make a procedure, that takes every character that are equal out of the resulting string, something like this:

<script type="text/javascript">
var a = "org.company.project.component.diff";
var b = "org.company.project.component.sub_component";
var i = 0;
while(a.charAt(i) == b.charAt(i)){
  i++;
}
alert(b.substring(i));
</script>

By the way it doesn't have a meaning to set Java and javascript as equals in any context, a popular way of putting it could be:

Java and javascript has four things in common: j - a - v - a !-)

roenving
A: 
function sub(a, b) {
  return [a, b].join('\x01').match(/^([^\x01]*)[^\x01]*\x01\1(.*)/)[2];
}

Though this relies on that the character with code 1 does not appear in any of those strings.

MizardX