Javascript比较字符串并获得结束差异

时间:2011-11-05 23:29:23

标签: javascript string

我有两个字符串。 字符串A:“快速的棕色狐狸” 字符串B:“快速的棕色狐狸跳过懒狗。”

字符串B将始终包含字符串A逐字。永远不会有“快速的黑狐狸”或“快速而快速的棕色狐狸”。

如何获得差异的“字符串C”“跳过懒狗。”

7 个答案:

答案 0 :(得分:13)

public Result doWork() {
    Log.i("wd","wd");

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,"ctx")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher))
            .setSmallIcon(R.drawable.logo)
            .setContentTitle("Title")
            .setContentText("Desc")
            .setContentIntent(pendingIntent);

    android.app.NotificationManager notificationManager =
            (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 , notificationBuilder.build());

    return Result.SUCCESS;
}

答案 1 :(得分:11)

请参阅下面的基本示例。 这可以很容易地修改/扩展以适应不同的行为。

var stringA = document.getElementById('string_a').textContent,
    stringB = document.getElementById('string_b').textContent,
    firstOccurrence = stringB.indexOf(stringA);

if(firstOccurrence === -1)
{
  alert('Search string Not found');
}
else
{
  var stringALength = stringA.length;
  var newString;

  if(firstOccurrence === 0)
  {
    newString = stringB.substring(stringALength);
  }
  else
  {
    newString = stringB.substring(0, firstOccurrence);
    newString += stringB.substring(firstOccurrence + stringALength);
  }

  document.getElementById('diff').textContent = newString;
}
<p>String A: <span id="string_a">The quick brown fox</span></p>
<p>String B: <span id="string_b">The quick brown fox jumps over the lazy dog</span></p>
<hr/>
<p>Difference: <span id="diff"></span></p>

答案 2 :(得分:7)

comu作为一个函数的答案......

function compareString( s1, s2, splitChar ){
    if ( typeof splitChar == "undefined" ){
        splitChar = " ";
    }
    var string1 = new Array();
    var string2 = new Array();

    string1 = s1.split( splitChar );
    string2 = s2.split( splitChar );
    var diff = new Array();

    if(s1.length>s2.length){
        var long = string1;
    }
    else {
        var long = string2;
    }
    for(x=0;x<long.length;x++){
        if(string1[x]!=string2[x]){
            diff.push(string2[x]);
        }
    }

    return diff;    
}
compareString( "?Yo=dude", "?Yo=Dude&do=roby", "&" ).join('\n');
compareString( "?Yo=Dude", "?Yo=Dude&do=roby", "&" ).join('\n');

注意:此答案解决了查找额外查询参数(基于另一个查询字符串)的问题,并且不是OP的确切答案。

答案 3 :(得分:6)

您需要将每个单词交叉检查到另一个单词。

var s1 = "The quick brown fox",
    s2 = "The quick brown fox jumped over the fence",
    string1 = new Array(),
    string2 = new Array(),
    diff = new Array(),
    longString;

string1 = s1.split(" ");
string2 = s2.split(" ");

if(s1.length > s2.length)
{
    longString = string1;
}
else
{
    longString = string2;
}

for(x = 0; x < longString.length; x++)
{
   if(string1[x] != string2[x])
   {
      diff.push(string2[x]);
   }
}

document.write("The difference in the strings is " + diff.join(" "));

答案 4 :(得分:1)

查看此网站以获取精彩的API,以查看字符串之间的区别:google-diff-match-patch 您可能需要根据需要检查UI。

答案 5 :(得分:0)

我找到了简单的方法。

代码如下:

&&

我尝试使用2个示例,带空格的文本和不带空格的文本,它工作正常。

当然,我已经尝试了您的示例,并且效果很好。

答案 6 :(得分:0)

const str1 = "The quick brown fox jumps over the lazy dog.";
const str2 = "he quick brown fox";

res = Str1.split(str2).join('');