Convert Camelcasetext to Title Case Text

Convert camelCaseText to Title Case Text

const text = 'helloThereMister';
const result = text.replace(/([A-Z])/g, " $1");
const finalResult = result.charAt(0).toUpperCase() + result.slice(1);
console.log(finalResult);

Convert camel case to sentence case in javascript

The below uses capitalised notation for acronyms; I don't agree with Microsoft's recommendation of capitalising when more than two characters so this expects the whole acronym to be capitalised even if it's at the start of the string (which technically means it's not camel case but it gives sane controllable output), multiple consecutive acronyms can be escaped with _ (e.g. parseDBM_MXL -> Parse DBM XML).

function camelToSentenceCase(str) {
return str.split(/([A-Z]|\d)/).map((v, i, arr) => {
// If first block then capitalise 1st letter regardless
if (!i) return v.charAt(0).toUpperCase() + v.slice(1);
// Skip empty blocks
if (!v) return v;
// Underscore substitution
if (v === '_') return " ";
// We have a capital or number
if (v.length === 1 && v === v.toUpperCase()) {
const previousCapital = !arr[i-1] || arr[i-1] === '_';
const nextWord = i+1 < arr.length && arr[i+1] && arr[i+1] !== '_';
const nextTwoCapitalsOrEndOfString = i+3 > arr.length || !arr[i+1] && !arr[i+3];
// Insert space
if (!previousCapital || nextWord) v = " " + v;
// Start of word or single letter word
if (nextWord || (!previousCapital && !nextTwoCapitalsOrEndOfString)) v = v.toLowerCase();
}
return v;
}).join("");
}

// ----------------------------------------------------- //

var testSet = [
'camelCase',
'camelTOPCase',
'aP2PConnection',
'JSONIsGreat',
'thisIsALoadOfJSON',
'parseDBM_XML',
'superSimpleExample',
'aGoodIPAddress'
];

testSet.forEach(function(item) {
console.log(item, '->', camelToSentenceCase(item));
});

Convert UPPERCASE to Title Case

Many of the useful existing NSString methods are available from Swift. This includes capitalizedString, which may just do exactly what you want, depending on your requirements.

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'

How do I convert a string to title case in android?

     /**
* Function to convert string to title case
*
* @param string - Passed string
*/
public static String toTitleCase(String string) {

// Check if String is null
if (string == null) {

return null;
}

boolean whiteSpace = true;

StringBuilder builder = new StringBuilder(string); // String builder to store string
final int builderLength = builder.length();

// Loop through builder
for (int i = 0; i < builderLength; ++i) {

char c = builder.charAt(i); // Get character at builders position

if (whiteSpace) {

// Check if character is not white space
if (!Character.isWhitespace(c)) {

// Convert to title case and leave whitespace mode.
builder.setCharAt(i, Character.toTitleCase(c));
whiteSpace = false;
}
} else if (Character.isWhitespace(c)) {

whiteSpace = true; // Set character is white space

} else {

builder.setCharAt(i, Character.toLowerCase(c)); // Set character to lowercase
}
}

return builder.toString(); // Return builders text
}

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.

How to convert camelCase to Camel Case?

"thisStringIsGood"
// insert a space before all caps
.replace(/([A-Z])/g, ' $1')
// uppercase the first character
.replace(/^./, function(str){ return str.toUpperCase(); })

displays

This String Is Good

(function() {
const textbox = document.querySelector('#textbox') const result = document.querySelector('#result') function split() { result.innerText = textbox.value // insert a space before all caps .replace(/([A-Z])/g, ' $1') // uppercase the first character .replace(/^./, (str) => str.toUpperCase()) };
textbox.addEventListener('input', split); split();}());
#result {  margin-top: 1em;  padding: .5em;  background: #eee;  white-space: pre;}
<div>  Text to split  <input id="textbox" value="thisStringIsGood" /></div>
<div id="result"></div>


Related Topics



Leave a reply



Submit