如果我们只想将字符串的首字母转换为大写或小写,可以使用substring()方法和toUpperCase()或toLowerCase()方法结合实现。下面是示例代码: Stringstr="hello world";StringfirstLetterUpperCase=str.substring(0,1).toUpperCase()+str.substring(1);StringfirstLetterLowerCase=str.substring(0,1).toLowerCase()+str.sub...
public static void main(String[] args){ System.out.println(new FirstLetterUppercase().upperFirstLatter("letter")); System.out.println(new FirstLetterUppercase().upperFirstLatter2("letter"));、 1. 2. 3. 4. 5. } /** * 方法一 * @param letter * @return */ public String upperFirstLatte...
packagecom.yiibai;importjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassMakeFirstLetterUppercase{publicstaticvoidmain(String[] args){Stringstr="this is a java test String upcase: 123string"; System.out.println(str);StringBufferstringbf=newStringBuffer();Matcherm=Pattern.compile("(...
The Java standard library has provided theString.toUpperCase()method, which allows us to convert all letters in a string to upper case. In this tutorial, we’ll learn how to convert a given string’s first character only to upper case. 2. Introduction to the Problem An example can explain...
public class FirstLetterUpperCase { public static void main(String[] args) { String str = "hello world"; String firstLetterCapitalized = capitalizeFirstLetter(str); System.out.println(firstLetterCapitalized); } public static String capitalizeFirstLetter(String str) { if (str == null || str....
The easiest way to capitalize the first character of each word of a string is by using Java 8 Stream API:String str = "welcome to java"; // uppercase first letter of each word String output = Arrays.stream(str.split("\\s+")) .map(t -> t.substring(0, 1).toUpperCase() + t....
String str = "hello world!"; // capitalize first letter String output = str.substring(0, 1).toUpperCase() + str.substring(1); // print the string System.out.println(output); // Hello world! The above example transforms the first letter of string str to uppercase and leaves the rest...
println("Original String: " + name); // get First letter of the string String firstLetStr = name.substring(0, 1); // Get remaining letter using substring String remLetStr = name.substring(1); // convert the first letter of String to uppercase firstLetStr = firstLetStr.toUpperCase()...
Main.java public class Main { public static void main(String[] args) { System.out.println("Hello World"); } } Try it Yourself » Example explainedEvery line of code that runs in Java must be inside a class. And the class name should always start with an uppercase first letter. ...
本教程文章將介紹如何使用 Java 將字串的首字母大寫。有一些常用的方法用於將給定string值的第一個字母轉換為大寫。不同的方法有upperCaseFirst()與toCharArray()、toUpperCase()和appendTail()方法,String.substring()方法和capitalize()函式與String.substring()方法。讓我們通過例項來討論每個方法的實現。