Java - String toUpperCase() 方法



描述

Java String toUpperCase() 方法用於將給定字串的所有字元轉換為大寫字母。

此方法有兩個多型變體,一個沒有任何引數,另一個使用給定Locale 資料型別概述的標準將字串轉換為大寫。這些方法的語法如下所示。

注意 - 請記住,大小寫對映基於Character 類標準的 Unicode 標準版本。由於大小寫對映不一定是 1:1 的,因此生成的新的字串的長度可能與原始字串的長度匹配,也可能不匹配。

語法

以下是Java String toUpperCase()方法的語法:

public String toUpperCase()
or,
public String toUpperCase(Locale locale)

引數

  • locale - 使用此區域設定的大小寫轉換規則。

返回值

此方法返回轉換為大寫的字串。

將字串轉換為大寫示例

以下示例演示了 Java String toUpperCase() 方法的使用,透過不傳遞任何引數將字串的給定字元轉換為大寫字母:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      
      // converts all lower case letters in to upper case letters
      String str1 = "This is TutorialsPoint";
      System.out.println("string value = " + str1.toUpperCase());    
      str1 = "www.tutorialspoint.com";
      System.out.println("string value = " + str1.toUpperCase());
   }
}

輸出

如果編譯並執行上面的程式,則輸出將顯示如下:

string value = THIS IS TUTORIALSPOINT
string value = WWW.TUTORIALSPOINT.COM

使用 Locale 將字串轉換為大寫示例

以下是一個使用 toUpperCase() 方法透過傳遞 Locale 引數將字串中的字元轉換為大寫的示例:

package com.turialspoint;

import java.util.Locale; 

public class StringDemo {
   public static void main(String[] args) {
      String str1 = "This is TutorialsPoint";   
      
      // using the default system Locale
      Locale defloc = Locale.getDefault();        
      
      // converts all lower case letters in to upper case letters
      System.out.println("string value = " + str1.toUpperCase(defloc));    
      str1 = "www.tutorialspoint.com";
      System.out.println("string value = " + str1.toUpperCase(defloc));
   }
}

輸出

如果編譯並執行上述程式,它將產生以下結果:

string value = THIS IS TUTORIALSPOINT
string value = WWW.TUTORIALSPOINT.COM

將包含非字母字元的字串轉換為大寫示例

讓我們建立一個另一個程式碼,該程式碼將生成包含字母、數字和符號的字元字串。在這個程式中,我們將確定 toUpperCase() 方法是否會影響數字和符號等非字母字元:

package com.turialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String s = "Welcome to @!! Tutorials point 77!!";
      System.out.println("The given string  is: " + s);
      String toUpper = s.toUpperCase();
      System.out.println("String after conversion is: " + toUpper);
   }
}

輸出

執行上述程式後,輸出如下所示:

The given string  is: Welcome to @!! Tutorials point 77!!
String after conversion is: WELCOME TO @!! TUTORIALS POINT 77!!

使用 Locale 將字串轉換為大寫示例

在下面的示例中,我們建立一個程式,透過將Locale物件作為引數傳遞給此方法,將字串“Welcome to turialspoint”轉換為大寫字母:

package com.turialspoint;

import java.util.Locale;

public class StringDemo {
   public static void main(String[] args) {
      String s = new String("Welcome to turialspoint");
      System.out.println("The given string is: " + s);
      
      // Create Locales with language "Eng" for English.
      Locale English = Locale.forLanguageTag("Eng");
      System.out.println("Uppercase letters in english: " + s.toUpperCase(English));
   }
}

輸出

上述程式碼的輸出如下所示:

The given string is: Welcome to turialspoint
Uppercase letters in turkish: WELCOME TO TURIALSPOINT
Uppercase letters in english: WELCOME TO TURIALSPOINT
java_lang_string.htm
廣告