Domanda

I have the below code:

sDocType = pqReq.Substring(0, pqReq.IndexOf(@"\t"));

The string pqReq is like this: "CSTrlsEN\t001\t\\sgprt\Projects2\t001\tCSTrl". But even though I can clearly see the t\ in the string, pqReq.IndexOf(@"\t") returns -1, so an error is thrown.

What's the correct way to do this? I don't want to split the string pqReq until later on in the code.

È stato utile?

Soluzione

Use \\t instead of \t. The \t is seen as a tab-character. sDocType = pqReq.Substring(0, pqReq.IndexOf(@"\t"));

Edit:

I didn't notice the \t being literal due to the @. But is your input string a literal string? If not, place an @ before the value of pqReq.

string pqReq = @"CSTrlsEN\t001\t\\sgprt\Projects2\t001\tCSTrl";
int i = pqReq.IndexOf(@"\t");
//i = 8

Altri suggerimenti

I can't reproduce this issue. The following code (.NET Fiddle here):

var pqReq=@"CSTrlsEN\t001\t\\sgprt\Projects2\t001\tCSTrl";
var idx=pqReq.IndexOf(@"\t");
Console.WriteLine(idx);
var sDocType = pqReq.Substring(0, idx);
Console.WriteLine(sDocType);

produces:

8
CSTrlsEN

Did you forget to prefix pqReq with @?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top