tags:

views:

165

answers:

6

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 (.

A: 

Check out the methods of the String class. Perhaps that will help determine what exactly you want to do.

Mamta Dalal
A: 

Something like:

 string breakMe =   "Eternal (woman)";
 string [] split = breakMe.Split( ' ' );

Would do what you are asking.

There are also other options one of which is with regex.

 string[] lines = Regex.Split(breakMe, "FANCY REGEX" );
Nix
A: 

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.

Vash
do you mean `s.Split(' ')`? There aren't any semicolons in the OP's string.
Matt Ellen
+1  A: 

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');
anishmarokey
I think you made a mistake there in the second code section. ;)
mizipzor
+1  A: 

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.

mizipzor
+5  A: 

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?

LukeH