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

时间:2017-02-14 08:49:58

标签: c# xamarin xamarin.forms

所以在我的程序中,我有一个Entry,如此:

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

这是一个属性绑定。

我在 Xamarin UWP 库中找到了这个System.Globalization.TextInfo.ToTitleCase,但我正在寻找适用于所有平台的通用解决方案

是否有一种算法可以应用于我的字符串,以便在字符串更改后将其应用于字符串?

5 个答案:

答案 0 :(得分:3)

我在以下链接中找到了一个很好的解决方案:

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

  

这个解决方案关注首字母大写和逃避单词,如,   a,在

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;
        }
    }
}

答案 1 :(得分:2)

您可以使用Linq分割所有单词并将第一个字母大写,如下所示:

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

为了使其正常工作,单词的分隔符必须始终是空格,它不会使用逗号,句号等...

答案 2 :(得分:2)

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

答案 3 :(得分:0)

您可以使用此

function isFunctionOfRightType(x: any): x is (conf: {})=>void {
   return (typeof x === 'function') && (x.length === 1); // or whatever test
}

// ... later

let property: this[keyof this] = utils.getProperty(this, name);
if (isFunctionOfRightType(property)) { property(conf); } // no error now

用法:

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;
    }

答案 4 :(得分:0)

这里的答案确实回答了您所问的问题,即“如何在用户键入时将用户键入的字符串转换为标题大小写”,但是我怀疑大多数用例仅涉及为字段设置默认值成为标题案例。我不是很容易找到答案,而是反复发现了这个问题,所以希望对其他人有帮助。

请参见Customizing the Keyboard docs

C#:

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

XAML:

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