1

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 a really compact way.

Another use-case is to find the diff between the strings:

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

I actually only want to remove the sections that are identical.

3
  • I don't see how JavaScript is relevant here... Sep 24, 2008 at 20:17
  • How isn't it? You never need to remove a string from another string in javascript? Granted, all solutions have been posted in java, but that doesn't invalidate the question. Sep 24, 2008 at 20:21
  • 1
    LOL - okay then, let's tag this as Java/JavaScript/C#/VB.NET/Python/Ruby/erlang/Smalltalk/Lisp/etc... My point is, JavaScript and Java have nothing in common other than syntax shared with C#, C, C++, etc. and this is not really a JavaScript solution. Sep 24, 2008 at 22:23

7 Answers 7

8

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.

3
  • Thanks Chris and Daniel (and all the rest of you). That was amazingly fast. Sep 24, 2008 at 20:04
  • 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. Sep 24, 2008 at 20:29
  • @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? Sep 24, 2008 at 20:49
6
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()

3

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 !-)

0

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

0

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.

0
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.

-2

This is a solution for the Javascript end of the question:

String.prototype.contracat = function(string){
    var thing = this.valueOf();
    for(var i=0; i<string.length;i++){
        thing=thing.replace(string.charAt(i),"");
    }
    return thing
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.