简体   繁体   中英

Backslash containing string not able find in other string

I have to take the \\r\\n from the registry and compare with another string, ie check variable if it contains the \\r\\n or not.

Please look in this code, its not giving "TRUE" result. Please fix this issues.

class Program
{
    static void Main(string[] args)
    {
        string check = "324214234\r\n";
        RegistryKey regKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\MyRegKey\\Settings");
        regKey.SetValue("ReceiveSplChar", @"\r\n");
        string value = regKey.GetValue("ReceiveSplChar").ToString();
        if (check.Contains(value))
            Console.WriteLine("TRUE");
    }
}

EDIT:

enter code here

        string SuffixValue = regKey.GetValue("ReceiveSplChar").ToString();
        if (SuffixValue != null)
            _RCVData = _SetSerialPort.ReadTo(SuffixValue);
        else
            _RCVData = _SetSerialPort.ReadLine();
        if (SerialDataEvent != null)
        {
            SerialDataEvent(_RCVData);
        }

its reading the character till the special character. it can be \\r\\n or \\t or \\u\u003c/i>

Problem : in your first assignmenet( string check = "324214234\\r\\n"; ) you are not escaping the special characters so \\r\\n treated as special characters and assigned string will be 324214234<newline> , but in your second assignment statement( regKey.SetValue("ReceiveSplChar", @"\\r\\n"); ) you are escaping special character with @ so the assigned string willbe intact 324214234\\r\\n

Solution :

Replace This:

string check = "324214234\r\n";

With This:

string check = @"324214234\r\n";

EDIT: if you want to find the ending character/symbol of any one of ( \\r\\n,\\n,\\t ) symbols you can use following sample code:

class Program
{
    static void Main(string[] args)
    {
        String str="12345\t";
        string endingSymbol=GetEndingSymbol(str);            
    }

    //returns the ending symbol of a given string
    static string GetEndingSymbol(string str)
    {

        if(str.IndexOf("\r\n")>=0)
        return "\r\n";
        else if(str.IndexOf("\n")>=0)
            return "\n";
        else if(str.IndexOf("\t")>=0)
            return "\t";
        return "";
    }
}

If you define a literal string without @ at the beginning, then \\r\\n will be treated as special characters. On the other hand, if you use @ before, then there is no escaping done and you get what you see (with the exception for " .

Try:

        static void Main(string[] args)
        {
            string check = @"324214234\r\n";
            RegistryKey regKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\MyRegKey\\Settings");
            regKey.SetValue("ReceiveSplChar", @"\r\n");
            string value = regKey.GetValue("ReceiveSplChar").ToString();
            if (check.Contains(value))
                Console.WriteLine("TRUE");
        }

Try this:

string check = "324214234\r\n";
string value = Environment.NewLine;
if (check.Contains(value))
    Console.WriteLine("TRUE");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM