1

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.

Dmitry
  • 13,797
  • 6
  • 32
  • 48
Doughy
  • 21
  • 1
  • 3
  • Do you want to Upper case only the second word? Are your strings composed by more than two words? – Steve Oct 03 '15 at 13:48
  • yes, I just want to uppercase the first letter of the second word. I'm trying to follow along in a book and use what they've shown to accomplish this. – Doughy Oct 04 '15 at 15:47
  • I can't use the toupper() function w/ a substring such as: int index; string name = "Jefferey steinberg"; index = name.IndexOf(' '); name = name[index + 1].ToUpper(); – Doughy Oct 04 '15 at 15:52

6 Answers6

5

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"));
    }
}
Alex
  • 21,273
  • 10
  • 61
  • 73
1
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.

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79
0

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
David BS
  • 1,822
  • 1
  • 19
  • 35
0

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();
}
ekostadinov
  • 6,880
  • 3
  • 29
  • 47
0

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);
}
Steve
  • 213,761
  • 22
  • 232
  • 286
0

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?

Doughy
  • 21
  • 1
  • 3