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
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
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)
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;
}
How to convert string to Title Case in Python?
Why not use title
Right from the docs:
>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"
If you really wanted PascalCase you can use this:
>>> ''.join(x for x in 'make IT pascal CaSe'.title() if not x.isspace())
'MakeItPascalCase'
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.
Related Topics
Most Elegant Way to Generate Prime Numbers
Show a Form Without Stealing Focus
"The Controls Collection Cannot Be Modified Because the Control Contains Code Blocks"
Capture Stored Procedure Print Output in .Net
How to Assign a Base Class Object to a Derived Class Reference with an Explicit Typecast
How to Deserialize JSON into Ienumerable<Basetype> with Newtonsoft JSON.Net
C# Okay with Comparing Value Types to Null
Conditional Operator Assignment with Nullable<Value> Types
ASP.NET Core Web API Exception Handling
How Is Math.Pow() Implemented in .Net Framework
Routing with Multiple Parameters Using ASP.NET MVC
Deserialize JSON with Known and Unknown Fields
How to Determine the Owner of a Process in C#
Entity Framework Stored Procedure Table Value Parameter
Difference Between 'Throw' and 'Throw New Exception()'