5

I have to convert the first letter of every word the user inputs into uppercase. I don't think I'm doing it right so it doesn't work but I'm not sure where has gone wrong D: Thank you in advance for your help! ^^

static void Main(string[] args)
    {
        Console.Write("Enter anything: ");
        string x = Console.ReadLine();

        string pattern = "^";
        Regex expression = new Regex(pattern);
        var regexp = new System.Text.RegularExpressions.Regex(pattern);

        Match result = expression.Match(x);
        Console.WriteLine(x);

        foreach(var match in x)
        {
            Console.Write(match);
        }
        Console.WriteLine();
    }

6 Answers6

7

If your exercise isn't regex operations, there are built-in utilities to do what you are asking:

System.Globalization.TextInfo ti = System.Globalization.CultureInfo.CurrentCulture.TextInfo;
string titleString = ti.ToTitleCase("this string will be title cased");

Console.WriteLine(titleString);

Prints:

This String Will Be Title Cased

If you operation is for regex, see this previous StackOverflow answer: Sublime Text: Regex to convert Uppercase to Title Case?

Community
  • 1
  • 1
Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
1

First of all, your Regex "^" matches the start of a line. If you need to match each word in a multi-word line, you'll need a different Regex, e.g. "[A-Za-z]".

You're also not doing anything to actually change the first letter to upper case. Note that strings in C# are immutable (they cannot be changed after creation), so you will need to create a new string which consists of the first letter of the original string, upper cased, followed by the rest of the string. Give that part a try on your own. If you have trouble, post a new question with your attempt.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
1
string pattern = "(?:^|(?<= ))(.)"

^ doesnt capture anything by itself.You can replace by uppercase letters by applying function to $1.See demo.

https://regex101.com/r/uE3cC4/29

vks
  • 67,027
  • 10
  • 91
  • 124
1

I would approach this using Model Extensions.

PHP has a nice method called ucfirst. So I translated that into C#

public static string UcFirst(this string s)
{
    var stringArr = s.ToCharArray(0, s.Length);
    var char1ToUpper = char.Parse(stringArr[0]
        .ToString()
        .ToUpper());

    stringArr[0] = char1ToUpper;

    return string.Join("", stringArr);
}

Usage:

[Test]
public void UcFirst()
{
    string s = "john";
    s = s.UcFirst();
    Assert.AreEqual("John", s);
}

Obviously you would still have to split your sentence into a list and call UcFirst for each item in the list.

Google C# Model Extensions if you need help with what is going on.

Andy
  • 2,124
  • 1
  • 26
  • 29
  • 1
    Nice, it worked, but had to figure out where to add the method. I created a folder `Extensions` with a class file `String.cs`, added `namespace ProjectName.Extensions { public static class StringExtension { ... } }`, within this class I added the method. To use the extension make sure `using ProjectName.Extensions` is added. – mikl Feb 01 '23 at 16:07
0

I hope this may help

public static string CapsFirstLetter(string inputValue)
    {
        char[] values = new char[inputValue.Length];
        int count = 0;
        foreach (char f in inputValue){
            if (count == 0){
                values[count] = Convert.ToChar(f.ToString().ToUpper());
            }
            else{
                values[count] = f;
            }
            count++;
        }
        return new string(values);
    }
Saubar
  • 135
  • 11
0

One more way to do it with regex:

string input = "this string will be title cased, even if there are.cases.like.that";
string output = Regex.Replace(input, @"(?<!\w)\w", m => m.Value.ToUpper());
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31