Implementation Method of JS Cleaning Duplicate Values in String

  • 2021-07-07 06:23:06
  • OfStack

In this paper, an example is given to describe the implementation method of JS to clear duplicate values in strings. Share it for your reference, as follows:


/// <summary>
///  Clear duplicate values in a string 
/// </summary>
/// <param name="Text"> String </param>
/// <param name="Label"> Labels (e.g.: | , ) </param>
function FilterRepeatStr(Text, Label) {
  var sarr = Text.split('' + Label + '');
  var idx = new Array();
  var tmp = new Array();
  var result = cm = '';
  for (var i = 0; i < sarr.length; i++) {
    sch = sarr[i].substr(0, 4);
    if (!In_Array(sch, tmp)) {
      idx[idx.length] = i;
      tmp[tmp.length] = sch;
    } else {
      idx[In_Array(sch, tmp) - 1] = i;
    }
  }
  for (var j = 0; j < idx.length; j++) {
    result += cm + sarr[idx[j]];
    cm = '' + Label + '';
  }
  return result;
//  alert(' String to be managed: ' + Text);
//  alert(' Results: ' + result);
}
function In_Array(need, arr) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == need) return (i + 1);
  }
  return false;
}

PS: Here we recommend another online tool with the same function for your reference:

Online Duplicate Removal Tool:

http://tools.ofstack.com/code/quchong

For more readers interested in JavaScript related content, please check the topics on this site: "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Traversal Algorithm and Skills", "Summary of json Operation Skills in JavaScript", "Summary of JavaScript Switching Effects and Skills", "Summary of JavaScript Animation Effects and Skills", "Summary of JavaScript Error and Debugging Skills" and "Summary of JavaScript Mathematical Operation Usage"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: