-2

So what I am trying to do is concatenate a USPS tracking number into a string. And the final string must have tracking number in quotes. For example, when I print the string it should look like:

<tracking userID=ABC trackingID="910236783468367367346738" deliveryNotification=false>

I have a list of tracking numbers as strings in an array(trackingIDs) and I am using a loop to create an above string for each tracking id.

 string finalString = null;
    for(int i = 0; i < trackingIDs.length; i++)
    {
        finalString = "<tracking userID=ABC trackingID=\"" + trackingIDs[i] + "\" deliveryNotification=false>";
          Console.WriteLine(finalString);
    }

This code produces:

<tracking userID=ABC trackingID=\"910236783468367367346738\" deliveryNotification=false>

For a smaller number, I could simply convert the number into an integer and everything would look fine. But I think these tracking numbers exceed the capacity of long so a conversion is not plausible. So basically my question is how do I get the "\" slashes to go away in this string ?

oneCoderToRuleThemAll
  • 834
  • 2
  • 12
  • 33
  • 3
    You get them to go away by not putting them there yourself in the first place. – Jon May 28 '14 at 18:55
  • 2
    I am using the \" \" as escape characters. Upon removing the slashes I get followingerror from Visual Studio:Error 1 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement – oneCoderToRuleThemAll May 28 '14 at 18:58
  • Isn't \" the escape sequence for the " character though? It seems like it should escape correctly, or am I missing something obvious (like an @ sign somewhere)? – BradleyDotNET May 28 '14 at 18:58
  • 5
    They shouldn't be outputting.... are you just checking the value in the debugger by mousing over? The IDE displays the escape character. – TyCobb May 28 '14 at 18:59
  • The code that you have provided is fine. A simple test, `Console.WriteLine(" – Servy May 28 '14 at 19:00
  • 2
    Not all USPS tracking numbers are just numbers some have letters to. – Bit May 28 '14 at 19:01
  • Yep, I was checking the debug mode. Just started using Visual Studio after 5 years and I forgot this detail. Thanks TyCobb ! – oneCoderToRuleThemAll May 28 '14 at 19:04
  • Great. Glad that helped you out. I added an answer for you to accept so this question doesn't go "unanswered". – TyCobb May 28 '14 at 20:14

6 Answers6

2

Based on your code it should escape correctly. I would think you're probably just looking at it in the debugger which will escape the quotations for your viewing pleasure.

Another way you might format the string without the \ escape character though is the following:

finalString = string.Format(@"<tracking userID=ABC trackingID=""{0}"" deliveryNotification=false>", trackingIDs[i]);

Additionally you might just use single-quotes instead.

finalString = string.Format(@"<tracking userID=ABC trackingID='{0}' deliveryNotification=false>", trackingIDs[i]);

I would not recommend using a numeric type to store your tracking numbers.

McAden
  • 13,714
  • 5
  • 37
  • 63
1

You don't have any issues in your code as it is correct. You are seeing this because you are mousing over the variable in Visual Studio which shows the escape character.

TyCobb
  • 8,909
  • 1
  • 33
  • 53
0

There's nothing wrong with this. Using your code (and capitalizing Length) I get this.

var trackingIDs = new string[]{"910236783468367367346738"};

 string finalString = null;
for(int i = 0; i < trackingIDs.Length; i++)
{
    finalString = "<tracking userID=ABC trackingID=\"" + trackingIDs[i] + "\" deliveryNotification=false>";
      Console.WriteLine(finalString);
}

output:

 <tracking userID=ABC trackingID="910236783468367367346738" deliveryNotification=false>
claudekennilol
  • 992
  • 2
  • 13
  • 26
0

How about this?

string[] trackingIDs = {"910236783468367367346738","810236783468367367346738","710236783468367367346738"};
    string finalString = null;
    for(int i = 0; i < trackingIDs.Length; i++)
    {
        finalString = "<tracking userID=ABC trackingID=\"" + trackingIDs[i] + "\" deliveryNotification=false>";
        Console.WriteLine(finalString);
    }

This is the output I get:

<tracking userID=ABC trackingID="910236783468367367346738" deliveryNotification=false>
<tracking userID=ABC trackingID="810236783468367367346738" deliveryNotification=false>
<tracking userID=ABC trackingID="710236783468367367346738" deliveryNotification=false>
Rahul Garg
  • 278
  • 1
  • 6
0

Try a different approach. What if you first break out the UPS tracking ID and get those into a List, then spin through those and build your final string with string.Format(). In your example I don't think you were properly escaping the quote marks either...

Something like this.

var trackingId = "123456789098888888893233432423423";
var finalString = string.Format("<tracking userID=ABC trackingID=\"{0}\" deliveryNotification=false>", trackingId);
GetFuzzy
  • 2,116
  • 3
  • 26
  • 42
-2

In C#, a double-quote is escaped using another one:

Refer to this SO quentsion: Can I escape a double quote in a verbatim string literal?

So, you don't need to use the backslash.

Community
  • 1
  • 1
jithinpt
  • 1,204
  • 2
  • 16
  • 33