Java string first char to upper

Uppercase First Character of String in Java

package net.javaguides.corejava; /** * Uppercase First Character of String in Java * @author Ramesh Fadatare * */ public class StringDemo < /** * Returns the input argument, but ensures the first character is * capitalized (if possible). * * @param in * the string to uppercase the first character. * @return the input argument, but with the first character capitalized (if * possible). * @since 1.2 */ public static String uppercaseFirstChar(String in ) < if ( in == null || in .length() == 0) < return in; > int length = in .length(); StringBuilder sb = new StringBuilder(length); sb.append(Character.toUpperCase( in .charAt(0))); if (length > 1) < String remaining = in .substring(1); sb.append(remaining); > return sb.toString(); > public static void main(String[] args) < String result = StringUtility.uppercaseFirstChar("java guides"); System.out.println(result); > >

Источник

Java String toUpperCase() Method Examples

Let’s look at some examples of using String toUpperCase() methods.

1. Simple String to Upper Case Conversion

jshell> Locale.getDefault() $1 ==> en_IN jshell> String str = "JavaString.net"; str ==> "JavaString.net" jshell> String strUpperCase = str.toUpperCase(); strUpperCase ==> "JAVASTRING.NET"

My system default locale is set to “en_IN”, so the conversion is using English locale rules.

Java String ToUpperCase Example

2. String to Upper Case Conversion with Locale

Let’s look at another example where we pass the Locale for the conversion rules.

jshell> String str = "title"; str ==> "title" jshell> String strUpper = str.toUpperCase(new Locale("tr", "tur")); strUpper ==> "TİTLE"

Notice that the second character is ‘\u005Cu0130’, which is the LATIN CAPITAL LETTER I WITH DOT ABOVE character.

Читайте также:  Как настроить свой сервер php

We can also change the default Locale using Locale.setDefault() method. In this case, we don’t need to pass the locale object every time.

jshell> String str = "title"; str ==> "title" jshell> Locale.setDefault(new Locale("tr", "tur")); jshell> String strUpperCase = str.toUpperCase(); strUpperCase ==> "TİTLE"

Java String ToUpperCase Locale Example

3. Locale Insensitive String to Upper Case using ROOT Locale

We can use Locale.ROOT to convert a string to upper case without locale-specific rules. The root locale language, country, and variant are empty strings.

jshell> String str = "title"; str ==> "title" jshell> String strUpperCase = str.toUpperCase(); strUpperCase ==> "TİTLE" jshell> String strUpperCase = str.toUpperCase(Locale.ROOT); strUpperCase ==> "TITLE" jshell> Locale.setDefault(Locale.ROOT); jshell> String strUpperCase = str.toUpperCase(); strUpperCase ==> "TITLE"

Java String ToUpperCase ROOT Locale

4. Change in String Length with toUpperCase()

Let’s look at an example where the string length is changed when it’s converted to upper case. There is a special character “eszett” (ß) whose upper case is “SS”.

jshell> String eszett = "ß"; eszett ==> "ß" jshell> String eszettUpperCase = "ß".toUpperCase(); eszettUpperCase ==> "SS" jshell> eszett.length() $16 ==> 1 jshell> eszettUpperCase.length() $17 ==> 2

Conclusion

Java String toUpperCase() method is used to convert the string characters to the upper case. It’s a locale-sensitive operation. It’s recommended to use ROOT locale for locale insensitive case conversion. It’s better to pass the Locale as an argument to get a consistent result and not relying on the system default locale.

References:

Источник

Convert each alternate character of a string to upper case in java

In this article, we will look at java program to convert alternate character of a string to upper case with explanation and sample output.
Algorithm
1. Get all characters of the string.
2. Iterate over the characters one by one.
3. In each iteration, check the position of character.
4. If its position is odd(such as 1, 3, 5, 7 etc.) since character position starts from 0, convert is to upper case.

public class CharacterConverter < public static void main(String[] args) < // initialize string String sample = "codippa"; // initialize string buffer to hold updated string StringBuffer updatedString = new StringBuffer(); // get array of characters in string char[] characters = sample.toCharArray(); // iterate over characters for (int index = 0; index < characters.length; index++) < // get current character char c = characters[index]; // check if position of this character is odd if (index % 2 != 0) < // convert it to upper case c = Character.toUpperCase(c); >// append character to string buffer updatedString.append(c); > System.out.println("Modified string is: " + updatedString.toString()); > >

From the output, it is clear that alternate characters of the string have been converted to upper case.

Читайте также:  Javascript отсчет дней от

If you just want to print the string to console, then there is no need of adding the characters to the StringBuffer object, just print them using System.out.print(c) inside the loop.

  1. Indexes of characters in a string start from 0 and move from left to right. Thus, character at left most end of a string will have an index of 0.
  2. If you want to convert alternate characters with first letter capitalized, then instead of checking for odd character, check for even character and capitalize only if the character is at even position since in that case, the characters to be capitalized will be at positions 0, 2, 4, 6 and so on.
  3. The above program capitalizes alternate characters but it is also possible to toggle the case of alternate characters, that is, capitalize them if they are in lower case and vice-versa.
    For this, check the case of character using Character.isLowerCase and Character.toUpperCase methods and convert the case accordingly.

Источник

Оцените статью