C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#で文字列をタイトルケースに変換するにはどうすればよいですか?


タイトルケースは、タイトルや見出しなど、主要な単語の最初の文字が大文字になっているテキストです。タイトルケースまたはヘッドラインケースは、公開された作品または芸術作品のタイトルを英語でレンダリングするために使用される大文字のスタイルです。タイトルケースを使用する場合、「マイナー」単語を除くすべての単語は、最初または最後の単語でない限り大文字になります。タイトル。

例のToTitleCaseの現在の実装では、入力文字列と同じ長さの出力文字列が生成されます。

例1

class Program{
   static void Main(string[] args){
      string myString = "wAr aNd pEaCe";
      TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
      Console.WriteLine("\"{0}\" to titlecase: {1}", myString, myTI.ToTitleCase(myString));
      Console.ReadLine();
   }
}

出力

"war and peace" to titlecase: War And Peace

例2

class Program{
   static void Main(string[] args){
      string[] values = {
         "a tale of three cities", "gROWL rescue",
         "inside the office", "sports and tennis",
         "The Return of Christmas Holmes"
      };
      TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
      foreach (var value in values)
      Console.WriteLine("{0} −−> {1}", value, ti.ToTitleCase(value));
      Console.ReadLine();
   }
}

出力

a tale of three cities −−> A Tale Of Three Cities
gROWL rescue −−> Growl Rescue
inside the office −−> Inside The Office
sports and tennis −−> Sports And Tennis
The Return of Christmas Holmes −−> The Return Of Christmas Holmes

  1. 文字列をJavaScriptオブジェクトに変換する方法は?

    以下は、文字列をJavaScriptオブジェクトに変換するコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> &nbs

  2. C#の文字列タイトルの場合とは何ですか?

    ToTitleCaseメソッドは、単語の最初の文字を大文字にするために使用されます。タイトルケース自体は、各主要単語の最初の文字を大文字にすることを意味します。 タイトルケースを取得する例を見てみましょう- 例 using System; using System.Globalization; class Demo {    static void Main() {       string str = "jack sparrow";       string res = CultureIn