I have a string. Example:
Eternal (woman)
I want to make it Eternal (Woman)
.
How I can do this in C#?
If I split by string[] mean = inf.Meaning.Split('(');
then I can't get (
.
I have a string. Example:
Eternal (woman)
I want to make it Eternal (Woman)
.
How I can do this in C#?
If I split by string[] mean = inf.Meaning.Split('(');
then I can't get (
.
Check out the methods of the String class. Perhaps that will help determine what exactly you want to do.
I think that the best way is to split it into an array and than trim and capitalize first character.
public static string doStaffSplit(string s) {
StringBuilder sb = new StringBuilder();
foreach(string word in s.Spilt('(') {
sb.Append(String.format("({0}",CultureInfo.CurrentCulture.TextInfo.ToTitleCase(word.Trim()));
}
return sb.ToString();
}
To capitalize
CultureInfo.CurrentCulture.TextInfo.ToTitleCase("string")' //res String
Should work, not tested.
Try this:
class Program
{
static void Main()
{
string str = "Eternal (woman)";
string[] s = str.Split('(');
string newString = string.Empty;
foreach (string sUpper in s)
{
newString += UppercaseFirst(sUpper);
}
newString = newString.Replace(" " ," (");
}
static string UppercaseFirst(string s)
{
// Check for empty string.
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
// Return char and concat substring.
return char.ToUpper(s[0]) + s.Substring(1);
}
}
You can also do:
"Eternal (woman)".Replace('w','W');
You don't need to split. The only thing that happens in your sample data is that w
has been capitalized. Thus, this does what you want:
"Eternal (woman)".Replace('w', 'W');
But I do urge you to update the question; add some context.
Your example data doesn't need to be split at all to achieve the desired results:
string foo = "Eternal (woman)";
string bar = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(foo);
Console.WriteLine(bar); // "Eternal (Woman)"
Is your real data any different? Do you actually need to split the string for some reason?