SoFunction
Updated on 2025-03-07

C# and js implement the method of removing duplicate records in textbox text box

This article shows the method of removing duplicate records in textbox text boxes in C# and js in the form of examples! The specific methods are as follows:

The following problem exists: The page has a textbox text box (multiple lines), for example, I enter: 111, 222, 333, 555 When 111 is input, this 111 will be automatically cleared, or the same record will be cleared before submitting a button!

The solution is as follows:

Split into an array by commas There is a Split() method
Then compare the equal ones inside and delete the one behind to form a new instance
Enter the new instance in format~

The C# code is as follows:

string temp_str = "111,222,333,111,444,222";
temp_str = (",", (temp_str, @"[,,]").Distinct());//111,222,333,444

In addition, this function can be implemented using js, and the implementation code is as follows:

<script type="text/javascript" language="javascript">
    $(document).ready(
      function() {
        $('#txt_test').keyup(
          function() {
            var txt = $('#txt_test').val();
            var str = new Array();
            str = (",");
            var strc = ((',') + 1, );
            for (var i = 0; i <  - 1; i++) {
              if (str[i] == strc) {
                alert(strc);
                var txt1 = (((','), ), "");
                $('#txt_test').val(txt1);
                break;
              }
            }
          }
        )
      }
    )
</script>

The foreground controls are as follows:

<asp:TextBox ID="txt_test" runat="server" TextMode="MultiLine"></asp:TextBox>

The only thing that's bad about writing this is
If the input form is: 111,
222,
111

If there is a carriage return in the middle of the arrangement, you need to make a separate judgment on the first line. Finally, you can write a paragraph to handle carriage return and spaces in this method. Readers can test this by themselves!