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

时间:2020-03-05 18:37:25  来源:igfitidea点击:

如何将字符串格式化为标题大小写?

解决方案:

这是在C#中执行此操作的简单静态方法:

public static string ToTitleCaseInvariant(string targetString)
{
    return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(targetString);
}

用什么语言?

在PHP中是:

ucwords()

例子:

$HelloWorld = ucwords('hello world');

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

在可能冒吸引挑剔者的风险的情况下,我会小心谨慎地自动将所有空格开头的单词转换为大写字母。

我至少会考虑为诸如条款和连词之类的例外情况实现一个字典。看哪:

convert first character to uppercase.
for each character in string,
    if the previous character is whitespace,
        convert character to uppercase.

This asssumes the "convert character to uppercase" will do that correctly regardless of whether or not the character is case-sensitive (e.g., '+').

If the language you are using has a supported method/function then just use that (as in the C# ToTitleCase method)

If it does not, then you will want to do something like the following:  

Read in the string  
Take the first word  
Capitalize the first letter of that word 1
Go forward and find the next word  
Go to 3 if not at the end of the string, otherwise exit  

1 To capitalize it in, say, C - use the ascii codes to find the integer value of the char and subtract 32 from it.

There would need to be much more error checking in the code (ensuring valid letters etc.), and the "Capitalize" function will need to impose some sort of "title-case scheme" on the letters to check for words that do not need to be capatilised ('and', 'but' etc. Here is a good scheme)

Here's a Perl solution http://daringfireball.net/2008/05/title_case

Here's a Ruby solution http://frankschmitt.org/projects/title-case

Here's a Ruby one-liner solution: http://snippets.dzone.com/posts/show/4702

'some string here'.gsub(/\b\w/){$&.upcase}

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

"Beauty and the Beast"

如果我们打算接受a-z和A-Z以外的字符,则这是一个较差的## 解决方案。

例如:ASCII 134:?,ASCII 143:?。
使用算术可以使我们:ASCII 102:f

使用库调用,不要假设我们可以对字符使用整数算术来获取有用的东西。 Unicode非常棘手。

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

To capatilise it in, say, C - use the ascii codes (http://www.asciitable.com/) to find the integer value of the char and subtract 32 from it.

在Perl中:

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

甚至在FAQ中也是如此。

在这里,我们有一个C ++版本。它有一组非大写单词,如代词和介词。但是,如果要处理重要的文本,我不建议自动执行此过程。

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

我认为使用CultureInfo并不总是可靠的,这是手动操作字符串的简便方法:

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

Excel中有一个内置公式PROPER(n)

很高兴看到我不必自己写它!

http://titlecase.com/具有API

在Silverlight中," TextInfo"类中没有" ToTitleCase"。

这是一个基于正则表达式的简单方法。

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

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;

代码数量不匹配