字符串替换

来源:互联网 发布:kmp算法next数组的含义 编辑:程序博客网 时间:2024/05/22 19:33

实现的功能是对固定的字符换成别的固定的字符串 

难点:当要替换的字符串是被替换串的一部分时,必须要做出判断否则会出现重

思路:把文章里各行读入数组,对各个位置开始的子字符串逐个进行判断。

如果被替换的长度大于替换串则先检查是否和被替换串相同,相同则替换。 

之后检查是否和要替换串相同,相同指针直接跳到这个词后边。 

如果前两个都不成立则指针向前移动。

如果要替换的长度大于被替换串则先检查是否和要替换串相同,相同指针直接跳到这个词后边。

之后检查是否和要替换串相同,相同则替换。

如果前两个都不成立则指针向前移动。 

<html>  <script type="text/javascript" language="javascript">       var mycars=new Array(10000);  var needToRepair =new Array(10000);  var count = 0;  var input = "1.txt";  var output = "2.txt";          function readFile()  {         count = 0;      var fso = new ActiveXObject("Scripting.FileSystemObject");         var f = fso.OpenTextFile(input,1);         var s = "";         while (!f.AtEndOfStream)       {            mycars[count] = f.ReadLine();          count++;       }       f.Close();     }         function right(toChange, start, end)  {     var temp = "";     if(end <= toChange.length)        temp = temp + toChange.substring(start, end);     return temp;  }      function left(toChange, start, end)  {     var temp = "";     if(start < toChange.length)        temp = temp + toChange.substring(start, end);     return temp;  }      function change(toChange, value1, value2)  {         var temp = toChange;      var head = 0;      var tail = head + value1.length;      var tail1 = head + value2.length;        if(value1.length < value2.length)  {       while(head < temp.length && tail <= temp.length)      {         // alert(temp.substring(head, tail));          var temp1 = "";                              if(head >= 0 && tail1 <= temp.length                  && temp.substring(head, tail1) == value2)          {              head = tail1;              tail = head + value1.length;              tail1 = head + value2.length;          }            else if(temp.substring(head, tail) == value1)          {              temp1 = right(temp, 0, head);              temp1 = temp1 + value2;              head = temp1.length;              temp1 = temp1 + left(temp, tail, temp.length);              tail = head + value1.length;              tail1 = head + value2.length;              temp = temp1;          }            else          {             head ++;             tail ++;             tail1 ++;          }          }  }  else  {      while(head < temp.length && tail <= temp.length)      {         // alert(temp.substring(head, tail));          var temp1 = "";                    if(temp.substring(head, tail) == value1)          {              temp1 = right(temp, 0, head);              temp1 = temp1 + value2;              head = temp1.length;              temp1 = temp1 + left(temp, tail, temp.length);              tail = head + value1.length;              tail1 = head + value2.length;              temp = temp1;          }          else if(head >= 0 && tail1 <= temp.length                  && temp.substring(head, tail1) == value2)          {              head = tail1;              tail = head + value1.length;              tail1 = head + value2.length;          }               else          {             head ++;             tail ++;             tail1 ++;          }          }  }      return temp;  }         function Replace()  {     var temp = "";     var temp1 = "";        for(var i = 0; i < count;  i++)     {        temp = mycars[i];              temp1 = change(temp, "ください", "下さい");        temp = temp1;        temp1 = change(temp, "の為", "のため");        temp = temp1;        temp1 = change(temp, "NRI社内", "社内");        temp = temp1;        temp1 = change(temp, "出来る", "できる");        temp = temp1;        temp1 = change(temp, "出来ません", "できません");        temp = temp1;        temp1 = change(temp, "御連絡", "ご連絡");        temp = temp1;        temp1 = change(temp, "の事", "のこと");        temp = temp1;        temp1 = change(temp, "問い合わせ", "お問い合わせ");        temp = temp1;        temp1 = change(temp, "お問合せ", "お問い合わせ");        temp = temp1;        temp1 = change(temp, "ヘルプデスク", "社内ヘルプデスク");        temp = temp1;        temp1 = change(temp, "セキュリティー", "セキュリティ");        temp = temp1;        temp1 = change(temp, "パソコン", "PC");        temp = temp1;        temp1 = change(temp, "手続", "手続き");        temp = temp1;        temp1 = change(temp, "社内社内", "社内");        temp = temp1;        mycars[i] = temp;          }  }                  function writeFile(){       var fso, f, s ;       fso = new ActiveXObject("Scripting.FileSystemObject");          f = fso.OpenTextFile(output,2,true);              for(var i = 0; i < count; i++)      {              f.WriteLine(mycars[i]);                 }        f.Close();    }               function run()  {      readFile();      Replace();      writeFile();  }      function myfunction()  {      input = document.getElementById("searchText").value;      output = "transfer_" + input;      run();  }  //  </script>         <input type="text" name="Text" id="searchText"  value="" maxlength="100">  <form>  <input type="button" onclick="myfunction()" value="test">  </form>          </html>    




0 0
原创粉丝点击