如何从C#中的字符串中查找输入键的索引(“\\\\ n”) [英] How Do I Find Index Of Enter Key("\r\n") from string In C#
问题描述
我挣扎了,在下面我已经详细提到了
Hi,
I got struggled,In below i have mentioned detaily
void _SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
sbSPBuffer.Append(_SerialPort.ReadExisting().ToString());
if (sbSPBuffer.ToString().IndexOf(Global.LHScannerPrefix.Trim()) != -1)
{
//Operation
}
}
Global.LHScannerPrefix="\r\n";
GLobal.LHScannerPrefix
配置为回车键,具体取决于扫描仪,有时它可能是tab键(\t);
我已经配置了带有后缀的串口扫描器作为回车键,所以每当我扫描任何条码时,它都会被扫描数据和输入键(\\\\ n);
之后我将删除回车键,然后我做我的操作。
当我尝试从扫描的条形码中找到索引( GLobal.LHScannerPrefix
)时,它找不到提到的值字符串。当我检查 GLobal.LHScannerPrefix
中的值时,它有\\\\\\ n
我怎样才能实现它。
GLobal.LHScannerPrefix
is configured as enter key,It will vary depends upon scanner, sometimes it may be tab key(\t);
I have configured my serial Port scanner with suffix as enter key, So whenever i scan any barcode ,it will comes scanned data along with enter key(\r\n);
After that I will Remove the enter key ,then i do my opeations.
When i try to find the Index (GLobal.LHScannerPrefix
) from scanned barcode ,its not find the mentioned value fom string. When i check the value in GLobal.LHScannerPrefix
it is having "\\r\\n"
How can i achieve it.
推荐答案
你可以使用String类的IndexOf方法:
You can use the IndexOf method of String class:
string test = "This is a test string.\r\n";
int index = test.IndexOf("\r\n"); // index = 22
这篇关于如何从C#中的字符串中查找输入键的索引(“\\\\ n”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!