Is There a Method for String Conversion to Title Case

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



Leave a reply



Submit