String literal still ending up with double slashes? [duplicate] - c#

This question already has answers here:
Why is \ character being doubled in my # string?
(3 answers)
Closed 8 years ago.
I have a small piece of code:
public static void Write(string filename){
string time = DateTime.Now.ToString("hh:mm tt");
int date = int.Parse(DateTime.Now.ToString("yyyyMMdd"));
string path = #"C:\Users\Public\" + filename;
}
If I debug and stop just after path is set it looks like “C:\\\Users\\\Public\\\filename.txt”.
Can anyone tell me why it has the double slashes? Is the '#' sign actually messing it up in this case?
How I may get it as “C:\Users\Public\filename.txt”

The double slashes are only there for the debugger, not your actual application. Your code is fine.

Related

How do I replace a special character [duplicate]

This question already has answers here:
string.Replace (or other string modification) not working
(4 answers)
Closed 12 months ago.
I'm trying to replace a special character "½" with ".5"
cell.InnerText="o208½-105u208½-109o208-110u209-110";
string tempStr=cell.InnerText;
if (cell.InnerText.Contains("½"))
{
cell.InnerText.Replace("½", ".5");
}
string tempStr1 = cell.InnerText;
but my C# .Replace isn't working , I get the same result.
String is an immutable type. Compiler creates a new string after replacing. So try just this
var innerText = "o208½-105u208½-109o208-110u209-110";
innerText= innerText.Replace("½", ".5");
result
before - o208½-105u208½-109o208-110u209-110
after - o208.5-105u208.5-109o208-110u209-110

c# string builder adding " char [duplicate]

This question already has answers here:
Escape double quotes in a string
(9 answers)
Closed 1 year ago.
i want to know how to insert umlaut(") to StringBuilder..
TEXT in HTML :::
<iframe src ="navigation.html" linkTarget = "iframe">
C#
StringBuilder htmlBuilder = new StringBuilder();
htmlBuilder.AppendLine("<iframe src = 'navigation.html' linkTarget = 'iframe'>")
With 'navigation.html' html not working correctly ... i need Append "navigation.html" ,
I want to hear a lot of yours ideas how it could be made ...
Like using chars .. etc.. :)
I think you're just missing the option of using \ to escape the double quote within a string literal:
htmlBuilder.AppendLine("<iframe src=\"navigation.html\" linkTarget=\"iframe\">");

Replacing Characters In C# String [duplicate]

This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 6 years ago.
I am attempting to replace characters in a string data type. Below is my code, but the ' is not being replaced with ''. Am I mis-understanding how the replace() function operates? What should I do in order to change the string to High Flying Picture''s?
This is the syntax I attempted.
public static void Test()
{
string strVar = "High Flying Picture's";
strVar.Replace("'", "''");
Console.WriteLine(strVar);
}
strVar = strVar.Replace("'", "''");
You need to reassign it.

Capitalizing the first letter of a string only [duplicate]

This question already has answers here:
Make first letter of a string upper case (with maximum performance)
(42 answers)
Closed 7 years ago.
I have already taken a look at such posts like:
Format to first letter uppercase
How to capitalise the first letter of every word in a string
But none of these seem to actually work. I would have thought to start with that there would just be a:
.Capitalize();
Like there is:
.Lower(); & .Upper();
Are there any documentation or references regarding converting to a string like the following?
string before = "INVOICE";
To then becoming:
string after = "Invoice";
I receive no errors using the way the posts solutions I read give me, however, the before still remains capitalized.
What about using ToUpper on the first char and ToLower on the remaining string?
string after = char.ToUpper(before.First()) + before.Substring(1).ToLower();
You can create a method that does something like this:
string UppercaseFirst(string str)
{
if (string.IsNullOrEmpty(str))
return string.Empty;
return char.ToUpper(str[0]) + str.Substring(1).ToLower();
}
And use it like this:
string str = "thISstringLOokSHorribLE";
string upstr = UppercaseFirst(str);
to get this:
Thisstringlookshorrible

Finding the substring [duplicate]

This question already has answers here:
How to get the last five characters of a string using Substring() in C#?
(12 answers)
Closed 9 years ago.
I have a string that could look like this: smithj_Website1 or it could look like this rodgersk_Website5 etc, etc. I want to be able to store in a string what is after the "_". So IE (Website1, Website5,..)
Thanks
Should be a simple as using substring
string mystr = "test_Website1"
string theend = mystr.SubString(mystr.IndexOf("_") + 1)
// theend = "Website1"
mystr.IndexOf("_") will get the position of the _ and adding one to it will get the index of the first character after it. Then don't pass in a second parameter and it will automatically take the substring starting at the character after the _ and stopping and the end of the string.
int startingIndex = inputstring.IndexOf("_") + 1;
string webSite = inputstring.Substring(startingIndex);
or, in one line:
string webSite = inputstring.Substring(inputstring.IndexOf("_") + 1);

Categories

Resources