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
Related
This question already has answers here:
Is there an easy way to return a string repeated X number of times?
(21 answers)
Closed 2 years ago.
Is there any way to interpolate variable several times without repeating?
For example:
var name = "bla";
Console.WriteLine($"foo {name:repeat:2} bar")
to print
foo blabla bar
I'm particularly interested in interpolating several line breaks instead of repeating {Environment.NewLine} several times in the interpolation mask like this:
$"{Environment.NewLine}{Environment.NewLine}"
public static string Repeat(this string s, int times, string separator = "")
{
return string.Join(separator, Enumerable.Repeat(s, times));
}
Then use:
Console.WriteLine($"foo {name.Repeat(2)} bar")
You could write an extension method for the string type, thats repeating its input. Then simply use this method within the curly braces.
You could also use
var name = "bla";
Console.WriteLine("foo {0}{0} bar", name);
// or
var s = String.Format("foo {0}{0} bar", name);
It will help you not repeating the same string, just index of it.
More about String Format
This question already has answers here:
How can I check if a string exists in another string
(10 answers)
Closed 5 years ago.
I'm working on a Existing Class file(.cs) which fetches a string with some data in it.
I need to check if the string contains a word. String has no blank spaces in it.
The string-
"<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>"
I need to check if the string contains 'ReleaseUserAuthPending' in it.
You can try this:
var strValue = "<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>";
if (strValue.Contains("ReleaseUserAuthPending"))
{
//Do stuff
}
Refer About String - Contains function
For your information: Contains function is case-sensitive. If you want to make this Contains function as case-insensitive. Do the following step from this link.
bool containsString = mystring.Contains("ReleaseUserAuthPending");
Try
String yourString = "<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>";
if(yourString.Contains("ReleaseUserAuthPending")){
//contains ReleaseUserAuthPending
}else{
//does not contain ReleaseUserAuthPending
}
This question already has answers here:
How can I check if a string contains a character in C#?
(8 answers)
Closed 5 years ago.
How I can check if my string has the value ".all" in. Example:
string myString = "Hello.all";
I need to check if myString has .all in order to call other method for this string, any ideas how I can do it?
you could use myString.Contains(".all")
More info here
Use IndexOf()
var s = "Hello.all";
var a = s.IndexOf(".all", StringComparison.OrdinalIgnoreCase);
If a = -1 then no occurrences found
If a = some number other than -1 you get the index (place in the string where it starts).
So a = 5 in this case
Simply call .Contains(".all") on the string object:
if (myString.Contains(".all")
{
// your code to call the other method goes here
}
There is no need for regex to do that.
Optionally, as mentioned by #ZarX in comments, you can check if the string ends with your keyword with .EndsWith(".all"), which will return true if the string ends with your keyword.
This question already has answers here:
How to remove a string from a string
(4 answers)
Closed 8 years ago.
I've this kind of string.
FullName:ae876ggfg777878848adgf877
And I want to remove "FullName:", so the output as follows:
ae876ggfg777878848adgf877
How can I do that?
I tried this:
var index = myText.IndexOf(":");
var result = myText.Remove(index);
But the output is like this:
FullName
Which I do not expect.
IndexOf returns the index of whatever string/character you give it, so in your case, the index of :.
Remove, according to the documentation:
Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.
So what's happening here is you're removing everything after and including the :
You should be using String.Replace:
string removed = myText.Replace("FullName:", "");
Use String.Substring() and get the start index by using String.IndexOf('character') + 1.
string s = "FullName:ae876ggfg777878848adgf877";
Console.WriteLine(s.Substring(s.IndexOf(':')+1));
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);