How do I get a non lowercase string after quotes in the titlecase condition

159 views Asked by At

In my article titles, I use CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower()); but I think, it is not working after double quotes. At least for Turkish.

For example, an article's title like this:

KİRA PARASININ ÖDENMEMESİ NEDENİYLE YAPILAN "İLAMSIZ TAHLİYE" TAKİPLERİNDE "TAKİP TALEBİ"NİN İÇERİĞİ.

After using the method like this:

private static string TitleCase(this string str)
{
   return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
}

var art_title = textbox1.Text.TitleCase(); It returns

Kira Parasının Ödenmemesi Nedeniyle Yapılan "İlamsız Tahliye" Takiplerinde "Takip Talebi"Nin İçeriği.

The problem is here. Because it must be like this:

... "Takip Talebi"nin ...

but it is like this:

... "Takip Talebi"Nin ...

What's more, in the MS Word, when I click "Start a Word Initial Expense," it's transforming like that

... "Takip Talebi"Nin ...

But it is absolutely wrong. How can I fix this problem?

EDIT: Firstly I cut the sentence from the blanks and obtained the words. If a word includes double quote, it would get a lowercase string until the first space after the second double quote. Here is the idea:

private static string _TitleCase(this string str)
{
    return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
}
public static string TitleCase(this string str)
{
    var words = str.Split(' ');
    string sentence = null;
    var i = 1;
    foreach (var word in words)
    {
        var space = i < words.Length ? " " : null;
        if (word.Contains("\""))
        {
            // After every second quotes, it would get a lowercase string until the first space after the second double quote... But how?
        }
        else
            sentence += word._TitleCase() + space;
        i++;
    }
    return sentence?.Trim();
}

Edit - 2 After 3 Hours: After 9 hours, I found a way to solve the problem. I believe that it is absolutely not scientific. Please don't condemn me for this. If the whole problem is double quotes, I replace it with a number that I think it is unique or an unused letter in Turkish, like alpha, beta, omega etc. before sending it to the ToTitleCase. In this case, the ToTitleCase realizes the title transformation without any problems. Then I replace number or unused letter with double quotes in return time. So the purpose is realized. Please share it in here if you have a programmatic or scientific solution.

Here is my non-programmatic solution:

public static string TitleCase(this string str)
{
    str = str.Replace("\"", "9900099");
    str = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
    return str.Replace("9900099", "\"").Trim();
}

var art_title = textbox1.Text.TitleCase();

And the result:

Kira Parasının Ödenmemesi Nedeniyle Yapılan "İlamsız Tahliye" Takiplerinde "Takip Talebi"nin İçeriği

2

There are 2 answers

0
Klaus Gütter On

Indeed, Microsoft documentation ToTitleCase states that ToTitleCase is (at least currently) not linguistically correct. In fact, it is REALLY hard to do this correctly (see these blog posts of the great Michael Kaplan: Sometimes, uppercasing sucks and "Michael, why does ToTitleCase suck so much?").

I'm not aware of any service or library providing a linguistically correct version.

So - unless you want to spend a lot of effort - you probably have to live with this inaccuracy.

0
241 On

You can find the apostrophe or quote character with RegEx and replace the character after it.

For apostrophe

Regex.Replace(str, "’(?:.)", m => m.Value.ToLower());

or

Regex.Replace(str, "'(?:.)", m => m.Value.ToLower());