c# format a string of characters so letters before number are capital and after are lowercase

I'm trying to format a string in c# and i m not sure if i shoudl be using regex or something like that basically if a modelname is md234GH and Dgh321Hh They need to be MD234gh and DGH321hh.

So capitals numbers lowercase

I tried doing the following:

TextInfo textInfo = new CultureInfo("en-GB", false).TextInfo;

foreach (var product in products)
    if (product.ModelName != null)
        product.ModelName = product.ModelName.ToLower();
        product.ModelName = textInfo.ToTitleCase(product.ModelName);

but this only makes the first letter capital.

Any advice appreciated


ToTitleCase is only mean to make the first letter of each word upper case. To do what you want, you'll need to split the string up and call .ToUpper and .ToLower as appropriate, e.g.:

Regex modelExpression = new Regex("^([A-Za-z]+)([0-9]+)([A-Za-z]+)$");

// Inside for loop...
Match m = modelExpression.Match(product.ModelName);
if (m.Success)
    product.ModelName = m.Groups[1].Value.ToUpper() 
         + m.Groups[2].Value 
         + m.Groups[3].Value.ToLower();

You could use a regex:


In a replace, something like:

model = Regex.Replace(model, @"(?i)^([a-z]+)(\d+)([a-z]+)$", delegate(Match m)
    return m.Groups[1].Value.ToUpper() + m.Groups[2].Value + m.Groups[3].Value.ToLower();

ToTitleCase will only make the first letter of each word upper case.

You need to use ToUpper to make all letters upper case.

There is nothing built in to do what you want, so you will need to make your own - parsing the string between the numerals and formatting each part.

A regular expression looks like the best bet:



var formatted = Regex.Replace(originalString,
                          m => m.Groups[1].Value.ToUpper() +
                          m.Groups[2].Value +

The result for "md234GH" is "MD234gh" and for "Dgh321Hh" it is "DGH321hh".

using System.Text.RegularExpressions;

Match m=Regex.Match("md234GH","(\\w+?)(\\d+)(\\w+)");

Regexes are a good match for this:


Now all that is needed is to use the regex api in C# and give it a delegate that does the replace. I've used a lambda as the delegate:

var result = Regex.Replace("md234GH", @"\b(\D+)(\d+)(\D+)\b", x =>
    + x.Groups[2].Value 
    + x.Groups[3].Value.ToUpper() 

Here is a solution in plain C. This assumes 8-bit ASCII or ANSI character set is used.

void convertModelName(char *name)
    char    *ptr;
    int     lcase = 0;

    for (ptr=name; *ptr; ptr++) {
        if (*ptr >= '0' && *ptr <= '9') {
            lcase = 1;
        } else {
            if (lcase) {
                *ptr |= 0x20;
            } else {
                *ptr &= 0xdf;

Need Your Help

downloading data in a loop, how often to save that information

r loops download save

I'm using R, but this question isn't specific to it. Suppose you've written some loop which downloads a url at each iteration. You want to to save this data, so you could do so each iteration or ...

Importing a MySQL InnoDB dump without locking tables

mysql import innodb mysqldump restore

What options do I have for importing a MySQL table using the InnoDB engine, where the table already exists, without losing read access to the existing table? Generally I am referring to situations ...