Make All Words Lowercase and the First Letter of Each Word Uppercase

Make all words lowercase and the first letter of each word uppercase

How would I be able to capitalize the first letter of each word and lower case the incorrectly capitalized letters?

ucwords() will capitilize the first letter of each word. You can combine it with strtolower() to first lowercase everything.

For example:

ucwords(strtolower('HELLO WORLD!')); // Hello World!

How do I lowercase any string and then capitalize only the first letter of the word with JavaScript?

A small sample:

function firstLetter(s) {

return s.replace(/^.{1}/g, s[0].toUpperCase());
}

firstLetter('hello'); // Hello

How to capitalize first letter and lowercase the rest while keeping the word capital if it is in fully uppercase - java

Most of the logic that you have posted works fine. The problem is words like "SENTENCE" because the logic that you are using to check the capitalization is incorrect.

The biggest problem is that you are trying to iterate over the words and check at the same time if that string is or not capitalize.

The easiest way is to separate concerns; try to check beforehand if the word is or not capitalized and act accordingly.

First create a method that just checks if a word is or not capitalized. For example:

public static boolean isUpper(String s, int start) {
for(int i = start; i < s.length(); i++) {
char c = s.charAt(i);
if(c == '.' || c == ' ')
return true;
if (!Character.isUpperCase(c))
return false;
}
return true;
}

This method receives a string (to be checked) and an int value (i.e., start) that tells the method from which part of the string should the checking start.

For the getSentenceCaseText method follow the following strategy. First check if the current word is capitalized:

 boolean capitalize = isUpper(text, pos);

if is capitalized the method should skip this word and move to the next one. Otherwise, it capitalizes the first character and lowers the remaining. Apply the same logic to all words in the text.

The code could look like the following:

public static String getSentenceCaseText(String text) {
int pos = 0;
StringBuilder sb = new StringBuilder(text);

// Iterate over the string
while (pos < sb.length()){
// First check if the word is capitalized
boolean capitalize = isUpper(text, pos);
char c = sb.charAt(pos);

// Make the first letter capitalized for the cases that it was not
sb.setCharAt(pos, Character.toUpperCase(c));
pos++;

// Let us iterate over the word
// If it is not capitalized let us lower its characters
// otherwise just ignore and skip the characters
for (;pos < sb.length() && text.charAt(pos) != '.' && text.charAt(pos) != ' '; pos++)
if(!capitalize)
sb.setCharAt(pos, Character.toLowerCase(sb.charAt(pos)));

// Finally we need to skip all the spaces
for(; pos < sb.length() && text.charAt(pos) == ' '; pos++ );
}

return sb.toString();
}

Use this strategy and this code as guide, build upon it and implement your own method.

How can I capitalize the first letter of each word in a string using JavaScript?

You are not assigning your changes to the array again, so all your efforts are in vain. Try this:

function titleCase(str) {   var splitStr = str.toLowerCase().split(' ');   for (var i = 0; i < splitStr.length; i++) {       // You do not need to check if i is larger than splitStr length, as your for does that for you       // Assign it back to the array       splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);        }   // Directly return the joined string   return splitStr.join(' '); }
document.write(titleCase("I'm a little tea pot"));

Convert all first letter to upper case, rest lower for each word

string s = "THIS IS MY TEXT RIGHT NOW";

s = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s.ToLower());

Return String by capitalizing every first letter of the each word and rest in lower case

I found the solution myself with same approach i started!! Here it is:

   function titleCase(str) {
var strSplit = (str.split(' '));
var newLetter = [];
var firstLetter;
var restLetter;
for(i=0; i <strSplit.length; i++){
firstLetter = strSplit[i].charAt(0).toUpperCase();
restLetter = strSplit[i].slice(1, str.Split).toLowerCase();
newLetter.push(firstLetter + restLetter);
}
return newLetter.join(" ");
}
titleCase("I'm a little tea pot");

How can I capitalize the first letter of each word in a string?

The .title() method of a string (either ASCII or Unicode is fine) does this:

>>> "hello world".title()
'Hello World'
>>> u"hello world".title()
u'Hello World'

However, look out for strings with embedded apostrophes, as noted in the docs.

The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result:

>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"


Related Topics



Leave a reply



Submit