3

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.

Abbas
  • 14,186
  • 6
  • 41
  • 72
Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148
  • You need to be checking for \\t I believe – laminatefish Dec 17 '13 at 15:33
  • 1
    http://ideone.com/FghOwM – Soner Gönül Dec 17 '13 at 15:35
  • I can't reproduce this, for this input: @"CSTrlsEN\t001\t\\sgprt\Projects2\t001\tCSTrl". Check the .NET Fiddle [here](http://dotnetfiddle.net/6b0meK). Did you perhaps forget to prefix the string with `@` ? – Panagiotis Kanavos Dec 17 '13 at 15:35
  • I've tried this myself with a simple 2 liner and get back "CSTrlsEN". Are you sure that you are "@" escaping your string in code? – Wolfwyrd Dec 17 '13 at 15:35
  • 2
    Is `pqReq` a literal? if it were declared inside your code, would it be declared as `pqReq = @"CSTrlsEN\t001\t\\sgprt\Projects2\t001\tCSTrl"` – AllenG Dec 17 '13 at 15:36
  • 1
    In your question, you first state you are looking for `t\ `, then describe searching for `\t`. Those are *different*. Please clarify. – abelenky Dec 17 '13 at 15:40
  • 1
    works for me. `var s = "CSTrlsEN\\t001\\t\\\\sgprt\\Projects2\\t001\\tCSTrl";` produce **exaclty** the same output as you stated, but still `s.IndexOf(@"\t")` returns **8**. – Ilya Ivanov Dec 17 '13 at 15:41
  • 1
    Same goes with `var s = @"CSTrlsEN\t001\t\\sgprt\Projects2\t001\tCSTrl";` – Ilya Ivanov Dec 17 '13 at 15:45

2 Answers2

2

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
Abbas
  • 14,186
  • 6
  • 41
  • 72
2

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 @?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236