The first idea to solve the problem is to split the input string into two substrings. For example, we can split theINPUTstring to “h” and “i there, Nice ….“. In other words, the first substring contains only the first character, and the other substring holds the remaining character...
Matcher; import java.util.regex.Pattern; public class Main{ public static String capitalize(final String word) { if (word.length() > 1) { return String.valueOf(word.charAt(0)).toUpperCase() + word.substring(1); }//from w w w .java2 s .co m return word; } } ...
The simplest way to capitalize the first letter of a string in Java is by using the String.substring() method: String str = "hello world!"; // capitalize first letter String output = str.substring(0, 1).toUpperCase() + str.substring(1); // print the string System.out.println(output...
This below example will show you, how to capitalize the first letter of a each word in a given string Java. Output:
To capitalize the first letter in a string is easy if you undertake some steps.First of all you should get the first letter of the string by setting the charAt() method at 0 index:Javascript charAt method1 2 let string = "w3docs.com"; console.log(string.charAt(0)); // Returns...
String firstLetter=word.substring(0,1); // Get remaining letter String remainingLetters=word.substring(1); capitalizeStr+=firstLetter.toUpperCase()+remainingLetters+" "; } System.out.println(capitalizeStr); } } Output: This Is Java Code That’s all about How to capitalize first letter in ...
Using String.replaceAll() Method Using Apache Commons TextIn this short guide, you will learn how to capitalize the first letter of each word in a string using Java. We have already learned to capitalize the first letter of a string in Java. But capitalizing each word in a string is a ...
importjava.util.Scanner;importnet.sourceforge.pinyin4j.PinyinHelper;publicclassMain{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.print("请输入一个字符串:");Stringinput=scanner.nextLine();scanner.close();charfirstChar=input.charAt(0);booleanisLetter=Character.isLet...
There is a number of ways to capitalize the first letter of the string in JavaScript. For example: "this is an example"->"This is an example" "the Atlantic Ocean"->"The Atlantic Ocean" ThetoUpperCase()method transforms all letters in a string to uppercase; we will use it in com...
const str = 'java2blog'; const substr = str.slice(1); console.log(substr); Output: ava2blog So we have used str.charAt(0) to extract first letter of String, str.toUpperCase() to capitalize first letter of String and str.slice(1) to concatenate remaining string to the result. 1 ...