locked
Comparing strings question RRS feed

  • Question

  • User350923160 posted

    Hello everyone,

    I have a simple web form that has two text boxes and a button.

    I want to check the content of the two strings against each other. The content will be only numbers from 1 to 7 in both strings. The condition I want is that if one string contains at least one character in the other string then return true. 

    For example,

    string test1 = "137";

    string test2 = "345";

    I want a way to tell me that this is true because 3 exists in both. 

    Thanks in advance!

    Friday, February 15, 2019 1:48 PM

Answers

  • User-821857111 posted

    var duplicate = test1.ToCharArray().Union(test2.ToCharArray()).Count() < test1.Length + test2.Length;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 15, 2019 3:21 PM
  • User-821857111 posted
    var input = "_M_W___";
    var output = "";
    for(var i = 1;i <= input.Length;i++)
    {
        if ((int)input[i-1] > 64 && (int)input[i-1] < 91){
            output += i.ToString();
        };
    }
    Console.WriteLine(output);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 18, 2019 10:01 AM

All replies

  • User-821857111 posted

    var duplicate = test1.ToCharArray().Union(test2.ToCharArray()).Count() < test1.Length + test2.Length;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 15, 2019 3:21 PM
  • User350923160 posted

    Thank you, it works great. 

    One more simple question. I am trying to create a small method that I pass it a string in the format of "_M_W___" or "__T_R__" and the method should return a string with "24" for the first string and "35" for the second string. Basically give me the integer number of the location of a letter other than the underscore "_"

    public string ConvertToNums(string input)
    {
    string output;
    . . .
    Return output; }

    Any ideas? 

    Thanks again

    Friday, February 15, 2019 3:48 PM
  • User-821857111 posted
    var input = "_M_W___";
    var output = "";
    for(var i = 1;i <= input.Length;i++)
    {
        if ((int)input[i-1] > 64 && (int)input[i-1] < 91){
            output += i.ToString();
        };
    }
    Console.WriteLine(output);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 18, 2019 10:01 AM
  • User350923160 posted

    This is perfect. 

    Thanks a lot. 

    Marking as fixed.

    Monday, February 18, 2019 4:55 PM