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 onl
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...
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 ...
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...
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 ...
This below example will show you, how to capitalize the first letter of a each word in a given string Java. Output:
class Solution { public int firstUniqChar(String s) { int [] letter = new int[26]; for (int i = 0; i < 26; i++) letter[i] = -1; char [] sc = s.toCharArray(); for (int i = 0; i < s.length(); i++) letter[sc[i] - 'a'] = (letter[sc[i] - 'a'] == -...
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...
defcapitalize_first_letter(string):returnstring[0].upper()+string[1:]text="hello world"capitalized_text=capitalize_first_letter(text)print(capitalized_text) 输出: 代码语言:txt 复制 Hello world 在这个示例中,我们定义了一个名为capitalize_first_letter的函数,它接受一个字符串作为参数,并返回一个新的字...