最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to 'subtract' one string from another? - Stack Overflow

programmeradmin0浏览0评论

Suppose I have the following two strings:

var value = "1-000-111";
var mask = " -   -";

I want to subtract the mask from the value. In other words I want something like this:

var output = subtract(value, mask);
// output should be 1000111

What is the best way to implement subtract()? I have written this, but that doesn't seem elegant to me.

function subtract(value, mask) {
    while (mask.indexOf('-') >= 0) {
        var idx = mask.indexOf('-');
        value = value.substr(0, idx) + value.substr(idx + 1);
        mask = mask.substr(0, idx) + mask.substr(idx + 1);
    }
    return value;
}

Does JavaScript have something built-in to acplish this? Note that, the masking characters are not limited to - (dash), but can be other characters as well, like +. But in a given case, the masking character can only be either - or +, which can therefore be sent to the subtract() function, which makes handling different masking character trivial. Also, the masking characters will be in arbitrary positions.

Any language-agnostic answers are also wele.

Suppose I have the following two strings:

var value = "1-000-111";
var mask = " -   -";

I want to subtract the mask from the value. In other words I want something like this:

var output = subtract(value, mask);
// output should be 1000111

What is the best way to implement subtract()? I have written this, but that doesn't seem elegant to me.

function subtract(value, mask) {
    while (mask.indexOf('-') >= 0) {
        var idx = mask.indexOf('-');
        value = value.substr(0, idx) + value.substr(idx + 1);
        mask = mask.substr(0, idx) + mask.substr(idx + 1);
    }
    return value;
}

Does JavaScript have something built-in to acplish this? Note that, the masking characters are not limited to - (dash), but can be other characters as well, like +. But in a given case, the masking character can only be either - or +, which can therefore be sent to the subtract() function, which makes handling different masking character trivial. Also, the masking characters will be in arbitrary positions.

Any language-agnostic answers are also wele.

Share Improve this question edited Mar 27, 2018 at 4:00 Sнаđошƒаӽ asked May 8, 2017 at 12:41 SнаđошƒаӽSнаđошƒаӽ 17.6k13 gold badges77 silver badges93 bronze badges 6
  • could the mask have other characters as hyphens to eliminate? – Nina Scholz Commented May 8, 2017 at 12:47
  • Your solution assumes all masks will use - is that the case? – epascarello Commented May 8, 2017 at 12:48
  • 1 No, mask may contain other characters as well. Also, masking characters may be in arbitrary positions. – Sнаđошƒаӽ Commented May 8, 2017 at 12:56
  • Does the position of the characters you want to remove matter? Does the number of characters to remove differ? – Lennholm Commented May 8, 2017 at 12:59
  • I'm assuming value.replace(/-/g ''); doesn't meet your requirements? – Lennholm Commented May 8, 2017 at 13:03
 |  Show 1 more ment

3 Answers 3

Reset to default 5

You could split the value string and filter the characters which are unequal to the character at the same position of the mask string.

Then join the array for a new string.

function subtract(value, mask) {
    return value.split('').filter(function (a, i) {
        return a !== mask[i];
    }).join('');
}

console.log(subtract("1-000-111", " -   -"));
console.log(subtract("foo1-000-111", "foo -   -"));

Just iterate through value and check if the corresponding value in mask is not -

    var value = '1-000-111';
    var mask  = ' -   -';
    
    var result = '';
    for (var i = 0; i < value.length; i += 1) {
        if (mask[i] !== '-') {
            result += value[i];
        }
    }
    
    console.log(result);

Mask can be any characters if you iterate through each character of value and pare to same position in mask:

Example using Array#reduce() on split() value

var value = "1-000-111";
var mask = " -   -";

var unmasked  = value.split('').reduce((a,c,i) => (mask[i] === c) ? a : a+c);

console.log(unmasked )

发布评论

评论列表(0)

  1. 暂无评论