First character of string to upper ASP.Net [duplicate] - c#

This question already has answers here:
Is there a native Proper Case string function in C#? [duplicate]
(5 answers)
Closed 9 years ago.
I'm attempting to return a username in ASP.net but would prefer to convert the first character to upper, for example if a username is 'test' I would want to return 'Test'.
Code to get username:
<h3>Welcome Home<strong><%: User.Identity.Name %></strong>.
Not 100% sure on how to implement this and I'm pretty sure it will just end up being something simple but any help would be appreciated.
Thanks
FIXED
h3>Welcome Home <strong><%: User.Identity.Name.ToUpper().Substring(0,1) + User.Identity.Name.ToLower().Substring(1) %></strong>.

System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(User.Identity.Name);

You can achive that with the following example
string input;
char.ToUpper(input[0]) + input.Substring(1);

char.ToUpper(User.Identity.Name[0]) + User.Identity.Name.Substring(1)

You don't need any code for that, just use plain CSS:
<h3>Welcome Home<strong style="text-transform: capitalize;"><%: User.Identity.Name %></strong></h3>
Working in all browsers as far as I could see.
Live test case.

Related

Removing Quotes and escaped quotes from string value [duplicate]

This question already has answers here:
C# String.Replace double quotes and Literals
(4 answers)
Closed 1 year ago.
I'm having a really embarrasing moment, as this feels like it's super simple but I just cannot seem to figure out what the best solution is, (or one at all for that matter).
I have a string like so:"\"123456\""
I want 123456
In C#, what is the quickest and easiest way of getting just the value from this? I've looked into using the Regex class and tried the .Unescape() method but I seem to have no success. Please help.
#CharlieFace's answer was the answer that worked for me.
string.Trim('\ "')
Thank you!

The + character in the parameter is returned as a space [duplicate]

This question already has answers here:
How to encode the plus (+) symbol in a URL
(6 answers)
Closed 1 year ago.
I created the custom link on the asp.net core project. And i tried to getting parameters in the link on the controller.
i need to prmtr1 and prmt2 values but '+' char in the prmtr1 return to ' '(space) value. I must get 'j+vNMbBKUGU=' but im getting 'j vNMbBKUGU='
link: blablabla/contract?prmtr1=j+vNMbBKUGU=&prmtr2=BIIOnHXUgGM=
if i change space char to + char, my problem fixed but i think it's not good idea. I hope you can help me. Thanks.
asp.net assumes that the "+" character is an encoded " " and therefore convert it to a space character.
In javascript you can use the function encodeURIComponent(string) to encode your parameters before sending.
You can read more about the function here: https://www.w3docs.com/snippets/javascript/how-to-encode-javascript-url.html#the-encodeuricomponent-function-5
And you can find more information about why this is happening here: https://stackoverflow.com/a/2678602/16154479

Replacing ' " ' in a string [duplicate]

This question already has answers here:
How to replace straight quotation mark (")
(3 answers)
Closed 6 years ago.
Some string variables have a " in the data which recks with my php code. I want to take it out in my C# code before the data gets passed to php. I just can't remember the way of doing it, but I tried with this :
line.Replace(#""", "");
This is not correct in Xamarin, what is the good syntax?
I guess this would work.
line.replace("\"","");
use \ to escape it.
line.Replace("\"", "");

Convert from %20 to normal character (replace) [duplicate]

This question already has answers here:
decode percent-encoded string c# .net
(4 answers)
Closed 7 years ago.
I've searched for this and cannot find any hint anywhere.
Basically there is a program that formats their file name like this
hello%20world%28hello%20world%29
which is supposed to mean hello world(hello world)
now im wondering is there any way that I could read every file name and anything that uses "%ascii" would be converted to normal text (e.g above).
Thanks in advance guys. I'm not that experienced in code and I'm hoping that someone could help me.
Use System.Uri.UnescapeDataString:
Uri.UnescapeDataString("hello%20world%28hello%20world%29");
Prints:
hello world(hello world)

Why string.format? [duplicate]

This question already has answers here:
String output: format or concat in C#?
(32 answers)
Why use String.Format? [duplicate]
(7 answers)
Closed 9 years ago.
Why shouldn't we simply use
string s=product.Name+" has been saved";
instead of:
string s=string.Format("{0} has been saved", product.Name);
One naive reason would be that it helps to prevent exactly the string formatting issue that you've presented in your original (unedited) question i.e.
string s=product.Name+"has been saved";
requires an extra space. The format method aids readability.
You could do that, no one say that you cannot. But mainly for readability, the second approach is prefered. It's even more obvious as soon as you concat more than 2 strings, it gets really messy, hard to read and mantain.
If you have many strings that you want to add, each + operation create new string.
For adding many strings you can use StringBuilder Class or String.Format

Categories