formatting strings with backslash

前端 未结 4 673
闹比i
闹比i 2021-01-22 22:15

I\'m a newbie to c# so hopefully this one isn\'t too hard for a few of you.

I\'m trying to build a string that has a \\ in it and I am having difficulty getting just one

相关标签:
4条回答
  • 2021-01-22 22:36
    string requiredString = string.Format(@"{0}\\{1}",str1,str2);
    
    0 讨论(0)
  • 2021-01-22 22:39

    I'm guessing you're looking at the values in the debugger and seeing that they have two slashes.

    That's normal. The debugger will show two slashes even though the actual string representation will only have one. Just another hump to get over when getting used to the debugger.

    Be assured that when you actually use your strings, they will still only have a single slash (using either of your methods).

    0 讨论(0)
  • 2021-01-22 22:47

    I'm guessing you're getting confused by the debugger.

    If you hover your mouse over a local variable in VS, strings will be escaped so a single \ will display as \\.

    To see what your string really is, output it somewhere for display (e.g., to the console) or hover your mouse on the variable, click on the arrow next to the little magnifying glass that appears, and select "Text Visualizer."

    0 讨论(0)
  • 2021-01-22 22:49

    If you're looking at these strings in the debugger (i.e., by hovering the mouse over the variable or using a watch), the debugger adds escape characters to the display string so that it's a valid string expression. If you want to view the string verbatim in this fashion, click on the magnifying glass on the right side of the tooltip or watch entry with the string in it.

    0 讨论(0)
提交回复
热议问题