String 将字符串格式化为标题大小写

String 将字符串格式化为标题大小写,string,language-agnostic,format,title-case,String,Language Agnostic,Format,Title Case,如何将字符串格式化为?在C#中有一个简单的静态方法: 用什么语言 在PHP中,它是: 例如: $HelloWorld = ucwords('hello world'); 在不使用现成函数的情况下,一种将字符串转换为标题大小写的超简单低级算法: 将第一个字符转换为大写。 对于字符串中的每个字符, 如果前一个字符是空白, 将字符转换为大写。 这就假定“将字符转换为大写”将正确执行此操作,而不管字符是否区分大小写(例如“+”)。如果您使用的语言具有受支持的方法/函数,则只需使用该方法(如在C#

如何将字符串格式化为?

在C#中有一个简单的静态方法:

用什么语言

在PHP中,它是:

例如:

$HelloWorld = ucwords('hello world');

在不使用现成函数的情况下,一种将字符串转换为标题大小写的超简单低级算法:


将第一个字符转换为大写。
对于字符串中的每个字符,
如果前一个字符是空白,
将字符转换为大写。


这就假定“将字符转换为大写”将正确执行此操作,而不管字符是否区分大小写(例如“+”)。

如果您使用的语言具有受支持的方法/函数,则只需使用该方法(如在C#
ToTitleCase
方法中)

如果没有,则需要执行以下操作:

  • 读入字符串
  • 第一个字
  • 将单词1的第一个字母大写
  • 继续,找到下一个单词
  • 如果不在字符串末尾,请转到3,否则退出
  • 1以大写形式表示,例如,C-使用查找字符的整数值并从中减去32

    代码中需要进行更多的错误检查(确保有效的字母等),“大写”函数需要在字母上施加某种类型的“标题大小写方案”,以检查不需要大写的单词(“and”、“but”等是一个很好的方案)

    下面是一个Perl解决方案

    这是一个Ruby解决方案

    下面是一个Ruby单行程序解决方案:


    “一行”所做的是使用正则表达式替换每个单词的第一个字符和大写版本。

    我会担心在有可能引起挑剔者愤怒的情况下自动将所有单词前面的空格上置

    我至少应该考虑为像文章和连词这样的例外情况实施字典。瞧:

    “美女与野兽”

    当涉及到专有名词时,事情变得更加丑陋

    要将其大写,比如说,C-使用ascii代码()找到字符的整数值并从中减去32

    如果您计划接受a-z和a-z以外的字符,这是一个糟糕的解决方案

    例如:ASCII 134:å,ASCII 143:Å。
    使用算术可以得到:ASCII 102:f


    使用库调用时,不要假设可以对字符使用整数算术来获取有用的内容。Unicode是。

    使用perl,您可以执行以下操作:

    my $tc_string = join ' ', map { ucfirst($\_) } split /\s+/, $string;
    
    在Perl中:

    $string =~ s/(\w+)/\u\L$1/g;
    

    <>这是FAQ中的。

    < P>这里有C++版本。它有一组不可大写的单词,比如前词和介词。但是,如果您要处理重要的文本,我不建议将此过程自动化

    #include <iostream>
    #include <string>
    #include <vector>
    #include <cctype>
    #include <set>
    
    using namespace std;
    
    typedef vector<pair<string, int> > subDivision;
    set<string> nonUpperCaseAble;
    
    subDivision split(string & cadena, string delim = " "){
        subDivision retorno;
        int pos, inic = 0;
        while((pos = cadena.find_first_of(delim, inic)) != cadena.npos){
            if(pos-inic > 0){
                retorno.push_back(make_pair(cadena.substr(inic, pos-inic), inic));
            }
            inic = pos+1;
        }
        if(inic != cadena.length()){
            retorno.push_back(make_pair(cadena.substr(inic, cadena.length() - inic), inic));
        }
        return retorno;
    }
    
    string firstUpper (string & pal){
        pal[0] = toupper(pal[0]);
        return pal;
    }
    
    int main()
    {
        nonUpperCaseAble.insert("the");
        nonUpperCaseAble.insert("of");
        nonUpperCaseAble.insert("in");
        // ...
    
        string linea, resultado;
        cout << "Type the line you want to convert: " << endl;
        getline(cin, linea);
    
        subDivision trozos = split(linea);
        for(int i = 0; i < trozos.size(); i++){
            if(trozos[i].second == 0)
            {
                resultado += firstUpper(trozos[i].first);
            }
            else if (linea[trozos[i].second-1] == ' ')
            {
                if(nonUpperCaseAble.find(trozos[i].first) == nonUpperCaseAble.end())
                {
                    resultado += " " + firstUpper(trozos[i].first);
                }else{
                    resultado += " " + trozos[i].first;
                }
            }
            else
            {
                resultado += trozos[i].first;
            }       
        }
    
        cout << resultado << endl;
        getchar();
        return 0;
    }
    
    #包括
    #包括
    #包括
    #包括
    #包括
    使用名称空间std;
    矢量细分;
    设置为不可大写;
    细分拆分(字符串和卡德纳,字符串delim=“”){
    第二,第二;
    int pos,inic=0;
    while((pos=cadena.find_first_of(delim,inic))!=cadena.npos){
    如果(位置inic>0){
    反击。推回(配对(卡德纳替补(inic、pos inic、inic));
    }
    inic=位置+1;
    }
    如果(inic!=cadena.length()){
    returno.push_back(制作_对(cadena.substr(inic,cadena.length()-inic,inic));
    }
    返回号;
    }
    字符串首字母大写(字符串和pal){
    pal[0]=toupper(pal[0]);
    返回pal;
    }
    int main()
    {
    不可大写。插入(“the”);
    不可大写。插入(“of”);
    不可大写。插入(“in”);
    // ...
    字符串linea,resultado;
    
    cout我认为使用CultureInfo并不总是可靠的,这是手动操作字符串的简单而方便的方法:

    string sourceName = txtTextBox.Text.ToLower();
    string destinationName = sourceName[0].ToUpper();
    
    for (int i = 0; i < (sourceName.Length - 1); i++) {
      if (sourceName[i + 1] == "")  {
        destinationName += sourceName[i + 1];
      }
      else {
        destinationName += sourceName[i + 1];
      }
    }
    txtTextBox.Text = desinationName;
    
    string sourceName=txtextbox.Text.ToLower();
    字符串destinationName=sourceName[0].ToUpper();
    对于(int i=0;i<(sourceName.Length-1);i++){
    if(sourceName[i+1]=“”){
    destinationName+=sourceName[i+1];
    }
    否则{
    destinationName+=sourceName[i+1];
    }
    }
    Text=designationname;
    
    Excel中有一个内置公式
    property(n)


    我很高兴看到我不用自己写了!

    http://titlecase.com/
    在Silverlight中有一个,
    TextInfo
    类中没有
    ToTitleCase

    这里有一个简单的基于正则表达式的方法

    注意:Silverlight没有预编译的正则表达式,但对我来说,性能损失不是问题

        public string TitleCase(string str)
        {
            return Regex.Replace(str, @"\w+", (m) =>
            {
                string tmp = m.Value;
                return char.ToUpper(tmp[0]) + tmp.Substring(1, tmp.Length - 1).ToLower();
            });
        }
    
    像往常一样出类拔萃:

    public static string ExcelProper(string s) {
        bool upper_needed = true;
        string result = "";
        foreach (char c in s) {
            bool is_letter = Char.IsLetter(c);
            if (is_letter)
                if (upper_needed)
                    result += Char.ToUpper(c);
                else
                    result += Char.ToLower(c);
            else
                result += c;
            upper_needed = !is_letter;
        }
        return result;
    }
    

    下面是Python的一个实现:


    这个实现的一个端口,我刚刚用C++完成了:

    在Java中,您可以使用以下代码

    public String titleCase(String str) {
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i == 0) {
                chars[i] = Character.toUpperCase(chars[i]);
            } else if ((i + 1) < chars.length && chars[i] == ' ') {
                chars[i + 1] = Character.toUpperCase(chars[i + 1]);
            }
        }
        return new String(chars);
    }
    
    publicstringtitlecase(stringstr){
    char[]chars=str.toCharArray();
    for(int i=0;i
    下面是一个简单的示例,说明如何执行此操作:

    public static string ToTitleCaseInvariant(string str)
    {
        return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str);
    }
    
    在C中#

    使用系统全球化;
    使用系统线程;
    受保护的无效页面加载(对象发送方、事件参数e)
    {  
    CultureInfo CultureInfo=Thread.CurrentThread.CurrentCulture;
    TextInfo TextInfo=cultureInfo.TextInfo;
    Response.Write(textInfo.ToTitleCase(“WelcometoHome
    ”); 回复。写(textInfo.ToTitleCase(“欢迎到家”); Response.Write(textInfo.ToTitleCase(“Welcome@to$home
    。替换(“@”和“)。替换(“$”和“); }
    在C语言中,您只需使用

    CultureInfo.InvariantCulture.TextInfo.ToTitleCase(str.ToLowerInvariant())
    
    • 不变的
    • 使用大写字符串

    完全不适用于代码表,例如德语中的umlauts(äöü)有不遵循这种模式的大写字母。它不再出现在常见问题解答中,因为它是错误的。想想这对收缩有什么影响:)这没有
    public String titleCase(String str) {
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i == 0) {
                chars[i] = Character.toUpperCase(chars[i]);
            } else if ((i + 1) < chars.length && chars[i] == ' ') {
                chars[i + 1] = Character.toUpperCase(chars[i + 1]);
            }
        }
        return new String(chars);
    }
    
    public static string ToTitleCaseInvariant(string str)
    {
        return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str);
    }
    
    using System.Globalization;  
    using System.Threading;  
    protected void Page_Load(object sender, EventArgs e)  
    {  
      CultureInfo cultureInfo   = Thread.CurrentThread.CurrentCulture;  
      TextInfo textInfo = cultureInfo.TextInfo;  
      Response.Write(textInfo.ToTitleCase("WelcometoHome<br />"));  
      Response.Write(textInfo.ToTitleCase("Welcome to Home"));  
    Response.Write(textInfo.ToTitleCase("Welcome@to$home<br/>").Replace("@","").Replace("$", ""));  
    }
    
    CultureInfo.InvariantCulture.TextInfo.ToTitleCase(str.ToLowerInvariant())