java 字符串首字符大写_Java字符串为大写

java 字符串首字符大写

Java String to uppercase conversion can be done using toUpperCase() method.

可以使用toUpperCase()方法将Java String转换为大写形式。

Java String to UpperCase (Java String to UpperCase)

  • Java String toUpperCase() method has two variants – toUpperCase() and toUpperCase(Locale locale).

    Java字符串toUpperCase()方法有两个变体– toUpperCase()toUpperCase(Locale locale)
  • Conversion of the characters to upper case is done by using the rules of default locale. Calling toUpperCase() function is same as calling toUpperCase(Locale.getDefault()).

    使用默认语言环境的规则将字符转换为大写。 调用toUpperCase()函数与调用toUpperCase(Locale.getDefault())
  • Java String to upper case method is locale sensitive, so use it carefully with strings that are intended to be used locale independent. For example programming language identifiers, HTML tags, protocol keys etc. Otherwise you might get unwanted results.

    Java字符串到大写方法对语言环境敏感,因此请谨慎使用它,该字符串应与语言环境无关。 例如编程语言标识符,HTML标记,协议密钥等。否则,您可能会得到不想要的结果。
  • To get correct upper case results for locale insensitive strings, use toUpperCase(Locale.ROOT) method.

    若要获取不区分区域设置的字符串的正确大写结果,请使用toUpperCase(Locale.ROOT)方法。
  • String toUpperCase() returns a new string, so you will have to assign that to another string. The original string remains unchanged because String is immutable.

    字符串toUpperCase()返回一个新字符串,因此您必须将其分配给另一个字符串。 原始字符串保持不变,因为String是不可变的
  • If locale is passed as null to toUpperCase(Locale locale) method, then it will throw NullPointerException.

    如果将locale作为null传递给toUpperCase(Locale locale)方法,则它将抛出NullPointerException

Java字符串到大写示例 (Java String to Uppercase Example)

Let’s see a simple example to convert a string to upper case and print it.

让我们看一个简单的示例,将字符串转换为大写并打印。

String str = "Hello World!";
System.out.println(str.toUpperCase()); //prints "HELLO WORLD!"

We can also use Scanner class to get user input and then convert it to uppercase and print it.

我们还可以使用Scanner类获取用户输入,然后将其转换为大写并打印出来。

Here is a complete example program to convert java string to uppercase and print it.

这是将Java字符串转换为大写并打印的完整示例程序。

package com.journaldev.string;

import java.util.Scanner;

public class JavaStringToUpperCase {

	public static void main(String[] args) {
		String str = "hello World!";

		String strUpperCase = str.toUpperCase();

		System.out.println("Java String to Upper Case: " + strUpperCase);

		readUserInputAndPrintInUpperCase();
	}

	private static void readUserInputAndPrintInUpperCase() {
		Scanner sc = new Scanner(System.in);
		System.out.println("Please write input String and press Enter:");
		String str = sc.nextLine();
		System.out.println("Input String in Upper Case = " + str.toUpperCase());
		sc.close();
	}

}

Below console output shows the sample execution of above program.

下面的控制台输出显示了上面程序的示例执行。

Java String to Upper Case: HELLO WORLD!
Please write input String and press Enter:
Welcome to JournalDev Tutorials
Input String in Upper Case = WELCOME TO JOURNALDEV TUTORIALS

That’s all for java string to uppercase conversion example.

这就是java字符串到大写转换示例的全部内容。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/18298/java-string-to-uppercase

java 字符串首字符大写

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值