C语言求姓氏首字母之和,如何在C中大写名字和姓氏的首字母?

17 回复  |  直到 4 年前

2bf58909a999e84465a22a090a8d2aa6?s=32&d=identicon&r=PG

1

255

4 年前

TextInfo.ToTitleCase()

将字符串的每个标记中的第一个字符大写。

如果不需要维护首字母缩略词大写,则应包括

ToLower()

.

string s = "JOHN DOE";

s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s.ToLower());

// Produces "John Doe"

如果当前区域性不可用,请使用:

string s = "JOHN DOE";

s = new System.Globalization.CultureInfo("en-US", false).TextInfo.ToTitleCase(s.ToLower());

18e8e5cf629cace8465683d4f41d87c0?s=32&d=identicon&r=PG

2

117

12 年前

CultureInfo.CurrentCulture.TextInfo.ToTitleCase("hello world");

8dd90cf47d9cd8d1663bb69f24f2d4a8.png

3

30

9 年前

String test = "HELLO HOW ARE YOU";

string s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(test);

以上代码不起作用…..

因此,将下面的代码转换为lower,然后应用函数

String test = "HELLO HOW ARE YOU";

string s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(test.ToLower());

9594239de591e6ea5c641a1f75ca350b?s=32&d=identicon&r=PG

4

12

7 年前

有些情况下

CultureInfo.CurrentCulture.TextInfo.ToTitleCase

无法处理,例如:撇号

'

.

string input = CultureInfo.CurrentCulture.TextInfo.ToTitleCase("o'reilly, m'grego, d'angelo");

// input = O'reilly, M'grego, D'angelo

string input = "o'reilly, m'grego, d'angelo";

input = Regex.Replace(input.ToLower(), @"\b[a-zA-Z]", m => m.Value.ToUpper());

// input = O'Reilly, M'Grego, D'Angelo

这个

正则表达式

例如,如果我们想处理

MacDonald

McFry

正则表达式变为:

(?<=\b(?:mc|mac)?)[a-zA-Z]

string input = "o'reilly, m'grego, d'angelo, macdonald's, mcfry";

input = Regex.Replace(input.ToLower(), @"(?<=\b(?:mc|mac)?)[a-zA-Z]", m => m.Value.ToUpper());

// input = O'Reilly, M'Grego, D'Angelo, MacDonald'S, McFry

如果我们需要处理更多的前缀,我们只需要修改组

(?:mc|mac)

,例如添加法语前缀

du, de

:

(?:mc|mac|du|de)

.

最后,我们可以意识到

正则表达式

也会匹配这个案例

MacDonald'S

最后一次

's

所以我们需要在

正则表达式

带着消极的眼光

(?

. 最后我们有:

string input = "o'reilly, m'grego, d'angelo, macdonald's, mcfry";

input = Regex.Replace(input.ToLower(), @"(?<=\b(?:mc|mac)?)[a-zA-Z](? m.Value.ToUpper());

// input = O'Reilly, M'Grego, D'Angelo, MacDonald's, McFry

8dd90cf47d9cd8d1663bb69f24f2d4a8.png

5

7

12 年前

mc和mac是美国常见的姓氏前缀,还有其他的。textinfo.toitlecase不处理这些情况,不应用于此目的。我是这样做的:

public static string ToTitleCase(string str)

{

string result = str;

if (!string.IsNullOrEmpty(str))

{

var words = str.Split(' ');

for (int index = 0; index < words.Length; index++)

{

var s = words[index];

if (s.Length > 0)

{

words[index] = s[0].ToString().ToUpper() + s.Substring(1);

}

}

result = string.Join(" ", words);

}

return result;

}

a007be5a61f6aa8f3e85ae2fc18dd66e?s=32&d=identicon&r=PG

7

4

3 年前

最直接的选择是使用

ToTitleCase

在.NET中可用的函数,该函数大部分时间都应注意名称。AS

edg

指出有些名字是不适用的,但这些名字是相当罕见的,所以除非你的目标是一个文化,这样的名字很常见,没有必要担心太多的事情。

但是,如果您不使用.NET语言,那么它取决于输入的外观-如果您的名字和姓氏有两个单独的字段,那么您可以使用子字符串将第一个字母大写,然后将其余的字母降低。

firstName = firstName.Substring(0, 1).ToUpper() + firstName.Substring(1).ToLower();

lastName = lastName.Substring(0, 1).ToUpper() + lastName.Substring(1).ToLower();

但是,如果提供多个名称作为同一字符串的一部分,则需要知道如何获取信息以及

split it

因此。所以,如果你得到一个像“约翰·多伊”这样的名字,你可以根据空格字符来拆分字符串。如果它是“doe,john”这样的格式,则需要根据逗号对其进行拆分。但是,一旦将其拆分,您只需应用前面显示的代码。

86c4cb6dfe926f6a600ca3d5abce58ae?s=32&d=identicon&r=PG

8

3

12 年前

cultureInfo.currentCulture.textinfo.toTitleBase(“我的名字”);

返回~我的名字

但如前所述,类似mcfly的名字仍然存在问题。

796eea3208806cfdc9a53cf2438b6219?s=32&d=identicon&r=PG

9

3

10 年前

我用我自己的方法来解决这个问题:

例如,“你好,世界。你好,这里是StackOverflow世界。“将是”你好世界。你好,这里是StackOverflow世界。regex \b(单词的开头)将执行此操作。

///

/// Makes each first letter of a word uppercase. The rest will be lowercase

///

///

///

public static string FormatWordsWithFirstCapital(string Phrase)

{

MatchCollection Matches = Regex.Matches(Phrase, "\\b\\w");

Phrase = Phrase.ToLower();

foreach (Match Match in Matches)

Phrase = Phrase.Remove(Match.Index, 1).Insert(Match.Index, Match.Value.ToUpper());

return Phrase;

}

84b5a2f1344918d728d89b026b769662?s=32&d=identicon&r=PG

10

2

12 年前

使用toitlecase的建议对于全部为大写的字符串不起作用。所以你必须在第一个字符上给toupper打电话,在剩下的字符上给tolower打电话。

9ada265062d3573e7e733356596f2b17?s=32&d=identicon&r=PG

11

2

12 年前

这门课有技巧。可以将新前缀添加到

前缀

静态字符串数组。

public static class StringExtensions

{

public static string ToProperCase( this string original )

{

if( String.IsNullOrEmpty( original ) )

return original;

string result = _properNameRx.Replace( original.ToLower( CultureInfo.CurrentCulture ), HandleWord );

return result;

}

public static string WordToProperCase( this string word )

{

if( String.IsNullOrEmpty( word ) )

return word;

if( word.Length > 1 )

return Char.ToUpper( word[0], CultureInfo.CurrentCulture ) + word.Substring( 1 );

return word.ToUpper( CultureInfo.CurrentCulture );

}

private static readonly Regex _properNameRx = new Regex( @"\b(\w+)\b" );

private static readonly string[] _prefixes = {

"mc"

};

private static string HandleWord( Match m )

{

string word = m.Groups[1].Value;

foreach( string prefix in _prefixes )

{

if( word.StartsWith( prefix, StringComparison.CurrentCultureIgnoreCase ) )

return prefix.WordToProperCase() + word.Substring( prefix.Length ).WordToProperCase();

}

return word.WordToProperCase();

}

}

ad348387d020a15171daed0ab492a49b?s=32&d=identicon&r=PG

12

1

12 年前

如果使用VS2K8,则可以使用扩展方法将其添加到字符串类:

public static string FirstLetterToUpper(this String input)

{

return input = input.Substring(0, 1).ToUpper() +

input.Substring(1, input.Length - 1);

}

8dd90cf47d9cd8d1663bb69f24f2d4a8.png

13

0

10 年前

为了解决本强调的一些问题,我建议先将字符串转换为小写,然后调用toitlecase方法。然后,您可以使用indexof(“mc”)或indexof(“o\”)来确定需要更具体注意的特殊情况。

inputString = inputString.ToLower();

inputString = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(inputString);

int indexOfMc = inputString.IndexOf(" Mc");

if(indexOfMc > 0)

{

inputString.Substring(0, indexOfMc + 3) + inputString[indexOfMc + 3].ToString().ToUpper() + inputString.Substring(indexOfMc + 4);

}

a25fde5fc8894f77c3baaef5af47bed7?s=32&d=identicon&r=PG

14

0

8 年前

我喜欢这样:

using System.Globalization;

...

TextInfo myTi = new CultureInfo("en-Us",false).TextInfo;

string raw = "THIS IS ALL CAPS";

string firstCapOnly = myTi.ToTitleCase(raw.ToLower());

02c79dd2ea591cef5e9ac31179fa1ae8?s=32&d=identicon&r=PG

15

0

8 年前

希望这对你有帮助。

String fName = "firstname";

String lName = "lastname";

String capitalizedFName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(fName);

String capitalizedLName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(lName);

8dd90cf47d9cd8d1663bb69f24f2d4a8.png

16

0

4 年前

public static string ConvertToCaptilize(string input)

{

if (!string.IsNullOrEmpty(input))

{

string[] arrUserInput = input.Split(' ');

// Initialize a string builder object for the output

StringBuilder sbOutPut = new StringBuilder();

// Loop thru each character in the string array

foreach (string str in arrUserInput)

{

if (!string.IsNullOrEmpty(str))

{

var charArray = str.ToCharArray();

int k = 0;

foreach (var cr in charArray)

{

char c;

c = k == 0 ? char.ToUpper(cr) : char.ToLower(cr);

sbOutPut.Append(c);

k++;

}

}

sbOutPut.Append(" ");

}

return sbOutPut.ToString();

}

return string.Empty;

}

f4f4b5a6f27f72b32d42463b1a25e0df?s=32&d=identicon&r=PG

17

-1

12 年前

正如EDG所指出的,您需要一个更复杂的算法来处理特殊的名称(这可能就是为什么许多地方强制所有内容都大写的原因)。

类似这种未经测试的C应该处理您请求的简单案例:

public string SentenceCase(string input)

{

return input(0, 1).ToUpper + input.Substring(1).ToLower;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值