String emptyOutput = StringUtils.capitalize(EMPTY_INPUT); assertEquals(EMPTY_EXPECTED, emptyOutput); String nullOutput = StringUtils.capitalize(null); assertNull(nullOutput); 6. Conclusion In this article, we’ve learned how to convert the first character of a given string to upper case. The code backing this article is av...
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...
capitalize the first letter in string Demo Codeimport java.util.regex.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); }//...
Capitalize first letter of String Here are the steps to convert first letter of string to uppercase in java Get first letter of String firstLetStr using str.substring(0,1). Get remaining String remLetStr using str.substring(1). Convert first letter of String firstLetStr to upper Case using...
Using Java 8 Streams 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...
Capitalize the First Letter of a String implementation. JavaScript provides many functions to work with Strings in general. We will leverage a few of them to achieve our aim. There are more than one way of accomplishing the task at hand and we shall take a look at a couple of them. ...
def capitalize_first_letter(string): return string[0].upper() + string[1:] text = "hello world" capitalized_text = capitalize_first_letter(text) print(capitalized_text) 输出: 代码语言:txt 复制 Hello world 在这个示例中,我们定义了一个名为 capitalize_first_letter 的函数,它接受一个字符串作为...
JavaScriptJavaScript String Video Player is loading. Current Time0:00 / Duration-:- Loaded:0% 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" ...
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 ...
This below example will show you, how to capitalize the first letter of a each word in a given string Java. Output: