简体   繁体   English

是否可以在 Xamarin.Forms 中将字符串转换为标题大小写?

[英]Is it possible to Convert a string to Title Case in Xamarin.Forms?

So in my program I have an Entry like so:所以在我的程序中,我有一个像这样的Entry

<Entry Text="{Binding testText}"/>

and this is bound to a property.这绑定到一个属性。

I have found this System.Globalization.TextInfo.ToTitleCase in the Xamarin UWP library, but I am looking for a generic solution for all platforms我在Xamarin UWP库中找到了这个System.Globalization.TextInfo.ToTitleCase ,但我正在寻找适用于所有平台的通用解决方案

Is there an algorithm I can apply to my string to apply Title Case to the string when it is changed?是否有一种算法可以应用于我的字符串,以便在更改字符串时将标题大小写应用于字符串?

I find a good solution on the following link:我在以下链接上找到了一个很好的解决方案:

https://www.codeproject.com/Tips/1004964/Title-Case-in-VB-net-or-Csharp https://www.codeproject.com/Tips/1004964/Title-Case-in-VB-net-or-Csharp

this solution take care about first-letter-capital and escape words such as the, a, in此解决方案处理首字母大写和转义词,例如 the、a、in

public static class StringExtensions
    {
        public static string ToTitleCase(this string s)
        {

        var upperCase = s.ToUpper();
        var words = upperCase.Split(' ');

        var minorWords = new String[] {"ON", "IN", "AT", "OFF", "WITH", "TO", "AS", "BY",//prepositions
                                   "THE", "A", "OTHER", "ANOTHER",//articles
                                   "AND", "BUT", "ALSO", "ELSE", "FOR", "IF"};//conjunctions

        var acronyms = new String[] {"UK", "USA", "US",//countries
                                   "BBC",//TV stations
                                   "TV"};//others

        //The first word.
        //The first letter of the first word is always capital.
        if (acronyms.Contains(words[0]))
        {
            words[0] = words[0].ToUpper();
        }
        else
        {
            words[0] = words[0].ToPascalCase();
        }

        //The rest words.
        for (int i = 0; i < words.Length; i++)
        {
            if (minorWords.Contains(words[i]))
            {
                words[i] = words[i].ToLower();
            }
            else if (acronyms.Contains(words[i]))
            {
                words[i] = words[i].ToUpper();
            }
            else
            {
                words[i] = words[i].ToPascalCase();
            }
        }

        return string.Join(" ", words);

    }

    public static string ToPascalCase(this string s)
    {
        if (!string.IsNullOrEmpty(s))
        {
            return s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower();
        }
        else
        {
            return String.Empty;
        }
    }
}

You can use Linq to split all the words and capitalize the first letter, something like this:您可以使用 Linq 拆分所有单词并将第一个字母大写,如下所示:

string input = "test of title case";
string output=String.Join(" ",input.Split(' ')
                         .ToList()
                         .Select(x => x = x.First().ToString().ToUpper() + x.Substring(1)));

In order for this to work, the separator of words must be a space always, it won't work with commas, periods etc...为了使它起作用,单词的分隔符必须始终是空格,它不适用于逗号、句点等...

TextInfo textInfo = new CultureInfo("pt-BR", false).TextInfo;
entryName.Text = textInfo.ToTitleCase(entryName.Text);

You can use this你可以用这个

public static string ToTitle(this string data)
    {
        if (!string.IsNullOrEmpty(data))
        {
            TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
            return string.Format(textInfo.ToTitleCase(data.ToLower()));
        }

        return data;
    }

Usage:用法:

string test = "hello there";
test.ToTitle();

The answers here do answer the question you asked, which was "how do I convert the string that the user types to title case as they type," but I suspect the majority of use cases involve just setting the default for the field to be title case.这里的答案确实回答了您提出的问题,即“我如何将用户键入的字符串转换为标题大小写”,但我怀疑大多数用例只涉及将字段的默认设置为标题案件。 I did not find the answer easily but have found this question repeatedly, so I hope this helps someone else.我没有轻易找到答案,但反复找到了这个问题,所以我希望这对其他人有所帮助。

See Customizing the Keyboard docs .请参阅自定义键盘文档

C#: C#:

MyEntry.Keyboard = Keyboard.Create(KeyboardFlags.CapitalizeWord);

XAML: XAML:

<Entry Placeholder="Enter text here">
    <Entry.Keyboard>
        <Keyboard xmlns:FactoryMethod="Create">
            <x:Arguments>
                <KeyboardFlags>CapitalizeWord</KeyboardFlags>
            </x:Arguments>
        </Keyboard>
    </Entry.Keyboard>
</Entry>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM