次のサンプル文字列があるとします。
PP12111 LOREM IPSUM TM ENCORE
LOREM PP12111 IPSUM TM ENCORE
LOREM IPSUM ENCORE TM PP12111
LOREM PP12111 PP12111 TM ENCORE
タイトルの大文字と小文字を設定し、数字と文字を含む文字列を大文字に変換する .NET RegEx は次のようになります (以下の注を参照)。
PP12111 Lorem Ipsum TM Encore
Lorem PP12111 Ipsum TM Encore
Lorem Ipsum Encore TM PP12111
Lorem PP12111 PP12111 TM Encore
または、すべてを Title Case に設定して開始することもできるので、数字と文字を含む文字列のみを大文字に設定する必要があります。
Pp12111 Lorem Ipsum TM Encore
Lorem Pp12111 Ipsum TM Encore
Lorem Ipsum Encore TM Pp12111
Lorem Pp12111 Pp12111 TM Encore
注: TM のバリアント (tm、Tm、tM) が存在する場合は、完全な大文字にする必要があります。TM は「lorem ipsum TM valor」または「lorem ipsum (TM) valor」の場合があります。
これは、機能する純粋な文字列操作方法です。正規表現ソリューションの方が適していると思いますか?
private static void Main( string[] args )
{
var phrases = new[]
{
"PP12111 LOREM IPSUM TM ENCORE", "LOREM PP12111 IPSUM TM ENCORE",
"LOREM IPSUM ENCORE TM PP12111", "LOREM PP12111 PP12111 TM ENCORE",
};
Test(phrases);
}
private static void Test( IList<string> phrases )
{
var ti = Thread.CurrentThread.CurrentCulture.TextInfo;
for( int i = 0; i < phrases.Count; i++ )
{
string p = ti.ToTitleCase( phrases[i].ToLower() );
string[] words = p.Split( ' ' );
for( int j = 0; j < words.Length; j++ )
{
string word = words[j];
if( word.ToCharArray().Any( Char.IsNumber ) )
{
word = word.ToUpper();
}
words[j] = word.Replace( " Tm ", " TM " ).Replace( "(Tm)", "(TM)" );
}
phrases[i] = string.Join( " ", words );
Console.WriteLine( phrases[i] );
}
}