Doughy
Doughy

Reputation: 21

How to uppercase a letter in the middle of a string?

I'm trying to uppercase the first letter in a string that is located after the first space:

string name = "Jeffrey steinberg";

I'm trying to uppercase the S in steinberg. I'm not sure how to do this. I've tried messing around w/ the toupper function but don't know how to reference the character "s" since c# strings aren't arrays like in c.

Upvotes: 1

Views: 3707

Answers (6)

ekostadinov
ekostadinov

Reputation: 6940

This is not true

c# strings aren't arrays like in c.

They are []char. You can itterate over them, get the length etc. Also are immutable. See MSDN:

String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects.

In the spirit of the context, such (I agree non-most-elegant) solution should work even for long full names:

public static string GetTitleCase(string fullName)
{
    string[] names = fullName.Split(' ');
    List<string> currentNameList = new List<string>();
    foreach (var name in names)
    {
        if (Char.IsUpper(name[0]))
        {
            currentNameList.Add(name);
        }
        else
        {
            currentNameList.Add(Char.ToUpper(name[0]) + name.Remove(0, 1));
        }
    }

   return string.Join(" ", currentNameList.ToArray()).Trim();
}

Upvotes: 0

Doughy
Doughy

Reputation: 21

I got this to work using the following snippit:

static void Main(string[] args)
    {
        int index;       
        string name = "Jefferey steinberg";
        string lastName;

        index = name.IndexOf(' ');
        lastName = name[index+1].ToString().ToUpper();
        name = name.Remove(index + 1, 1);
        name = name.Insert(index + 1, lastName);

        Console.WriteLine(name);
        Console.ReadLine();
    }

Is there a better way to do this?

Upvotes: 0

Steve
Steve

Reputation: 216293

If you want to replace a single char inside a string, you cannot act on that string replacing an element in the array of chars. Strings in NET are immutable. Meaning that you cannot change them, you could only work on them to produce a new string. Then you could assign this new string to the same variable that contains the source string effectively replacing the old one with the new one

In your case you affirm that you want to change only the first char of the second word of your input string. Then you could write

// Split the string in its word parts
string[] parts = name.Split(' ');

// Check if we have at least two words
if(parts.Length > 1)
{
    // Get the first char of the second word
    char c = parts[1][0];

    // Change char to upper following the culture rules of the current culture
    c = char.ToUpper(c, CultureInfo.CurrentCulture);

    // Create a new string using the upper char and the remainder of the string
    parts[1] = c + parts[1].Substring(1);

    // Now rebuild the name with the second word first letter changed to upper case
    name = string.Join(" ", parts);
}

Upvotes: 0

Anirudha Gupta
Anirudha Gupta

Reputation: 9289

string name = "Jeffrey steinberg";

TextInfo myTI = new CultureInfo("en-US",false).TextInfo;

 myTI.ToTitleCase(name)

Some culture doesn't make ToTitleCase work so it's better to use en-us to make titlecase.

Upvotes: 1

Alex
Alex

Reputation: 21766

You can use TitleCase for this:

using System;

public class Program
{
    public static void Main()
    {
       Console.WriteLine(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase("Jeffrey steinberg"));
    }
}

Upvotes: 5

David BS
David BS

Reputation: 1892

You may try the function ToTitleCase, since it will be better than search for spaces in strings and capitalize the next letter.

For instance:

 string s = "Jeffrey steinberg smith";
 TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
 string uppercase = ti.ToTitleCase(s);

 Result:  Jeffrey Steinberg Smith

Upvotes: 0

Related Questions