Is there a simple way to convert java string to title case including diacritics, without third party library
I use this method with regex:
public static void main(String[] args) {
System.out.println(titleCase("JEAN-CLAUDE DUSSE"));
System.out.println(titleCase("sinéad o'connor"));
System.out.println(titleCase("émile zola"));
System.out.println(titleCase("O'mALLey"));
}
public static String titleCase(String text) {
if (text == null)
return null;
Pattern pattern = Pattern.compile("\\b([a-zÀ-ÖØ-öø-ÿ])([\\w]*)");
Matcher matcher = pattern.matcher(text.toLowerCase());
StringBuilder buffer = new StringBuilder();
while (matcher.find())
matcher.appendReplacement(buffer, matcher.group(1).toUpperCase() + matcher.group(2));
return matcher.appendTail(buffer).toString();
}
I have tested with your strings.
Here is the results to output:
Jean-Claude Dusse
Sinéad O'Connor
Émile Zola
O'Malley
Convert string to title case with exceptions for articles (a, an, the..etc)
The Array.includes(String) function returns if the string is a part of the array.
This should work,
for(i = 0; i < splitStr.length; i++) {
// Check if our word is a part of the exceptions list
if(i>0 && exceptions.includes(splitStr[i]))
// if it's an exception skip the capatilization
continue;
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
Credit for i>0 condition goes to @Ringo, i didn't think about that before. He's right that first word should always be capitalized regardless.
Converting string to title case
MSDN : TextInfo.ToTitleCase
Make sure that you include: using System.Globalization
string title = "war and peace";
TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
title = textInfo.ToTitleCase(title);
Console.WriteLine(title) ; //War And Peace
//When text is ALL UPPERCASE...
title = "WAR AND PEACE" ;
title = textInfo.ToTitleCase(title);
Console.WriteLine(title) ; //WAR AND PEACE
//You need to call ToLower to make it work
title = textInfo.ToTitleCase(title.ToLower());
Console.WriteLine(title) ; //War And Peace
How to convert string to Title Case?
In the function the string B is an empty string so there is no B[0], however you can assign a char to a string so the following should work
string Title_Case (const string A){
char First_capital = static_cast<int>(A[0]) - 32;
string B;
B = First_capital;
for ( int i =1; i <A.size(); i++){
if( A[i-1] == ' ' && A[i] >= 'a' && A[i] <= 'z'){
char capital = A[i] - ( 'a' - 'A');
B += capital;
continue;
}
else
B = B + A[i];
}
return B;
}
Javascript - Adjusting string to title case logic
letterToCapitalize
finds the first occurrence of [a-zA-Z0-9_]
in each word you're trying to capitalize (the first suitable letter of).
In your code, you must remember that .replace(...)
actually returns a brand new string. It does not modify the string in place (in JavaScript, primitive data-types, of which strings are one, are not modifiable). The problem was you weren't assigning this new string to a variable that you were logging out.
let title = "Adjusting the title (case and test) on tv"
let titleCase = title.toLowerCase().split(' ').map((s) => {
let letterToCapitalize = s.match(/\w/)[0];
return s.replace(letterToCapitalize, letterToCapitalize.toUpperCase())
}).join(' ');
titleCase = titleCase.replace(/and/ig, '&').replace("Tv", "TV");
console.log(titleCase)
Built-in method to convert a string to title case in .NET Core?
.NET Standard 2.0 added TextInfo.ToTitleCase (source), so you can use it in .NET Core 2.0.
For .NET Core 1.x support, however, you are out of luck.
Related Topics
Java's Date(...) Constructor Is Deprecated; What Does That Mean
How to Keep the Iteration Order of a List When Using Collections.Tomap() on a Stream
Is There a Difference in Removing the Curly Braces from If Statements in Java
Simple Way to Count Character Occurrences in a String
In Java, How to Get the Difference in Seconds Between 2 Dates
How to Wait Until an Element Is Present in Selenium
How to List All Classes Loaded in a Specific Class Loader
Filtering Database Rows with Spring-Data-JPA and Spring-Mvc
How to Set Classpath When I Use Javax.Tools.Javacompiler Compile the Source
How to Get Backspace \B to Work in Eclipse's Console
Hexadecimal to Integer in Java
What's in an Eclipse .Classpath/.Project File
Java Thread Priority Has No Effect
Call a Method of Subclass in Java
How to Change Font Size in Eclipse for Java Text Editors
What Are the Pros and Cons of Performing Calculations in SQL VS. in Your Application