Basic Javascript Algorithm [duplicate] - javascript

This question already has answers here:
Capitalize words in string [duplicate]
(21 answers)
Closed 7 years ago.
What to do - Capitalize the first letter of the words in a sentence.
So, I solved it and was wondering is there any way to do it without making it an array with .split().
What I tried without turning it into a array -
The logic - First, turn everything into lowercase. Then scan the sentence with a for loop, if you find a space, capitalize the next character.
function titleCase(str) {
str = str.toLowerCase();
for(i=0;i<str.length;i++) {
if(str[i]===" ") {
str = str.charAt[i+1].toUpperCase();
return str;
}
}
}
titleCase("I'm a little tea pot", "");
That code doesn't even run.

I just used split() and replace to do that. You can have a look at my code.
function titleCase (str)
{
str = str.split(' ');
for(var i=0;i<str.length;i++)
{
str[i] = str[i].replace(str[i][0],str[i][0].toUpperCase())
}
return str.join(' ');
}
var mainString ="i am strong!";
titleCase(mainString);

Here is one using replace + with a regex:
/**
* #summary Uppercase the first letter in a string.
* #returns {string}
*/
function uppercaseFirstLetters(string) {
return string.replace(/[a-zA-Z]*/g, function(match) {
return match.charAt(0).toUpperCase() + match.substr(1).toLowerCase();
})
}

Related

JavaScript-Creating a Function to capitalise the first letter of a string [duplicate]

This question already has answers here:
Convert string to Pascal Case (aka UpperCamelCase) in Javascript
(5 answers)
Closed 2 years ago.
I'm having trouble in creating a function to capitalise the first letter of a string. I can capitalise the string when I enter words in lowercase, but not in uppercase. I really appreciate some assistance with this problem, please see my script below:
function captialise(str) {
str = prompt("Enter a string");
console.log(str[0].toUpperCase() + str.substring(1))
}
captialise();
Uppercase the first character, lowercase the rest. The snippet uses a bit more modern scripting (the function uses a template literal)
const capitalize = str => str.slice
? `${str.slice(0,1).toUpperCase()}${str.slice(1).toLowerCase()}`
: str;
const str1 = `someSTRING`;
const str2 = `someotherstring`;
const str3 = `s23omeotherstring`;
const str4 = prompt('enter something');
console.log(capitalize(str1));
console.log(capitalize(str2));
console.log(capitalize(str3));
console.log(capitalize(str4));
There are many ways to do it, and I believe it was asked a lot of time here before,
This one is with one line:
function captialise(str) {
str = prompt("Enter a string");
console.log(`${str[0].toUpperCase()}${str.substring(1).toLowerCase()}`);
}
captialise();
The logical problem is that you're capitalising the first part yes, but you're NOT lowering the other parts
function captialise(str) {
str = prompt("Enter a string");
console.log(str[0].toUpperCase() + str.substring(1).toLowerCase())
}
captialise();
It is recommended to have a function return what you name it.
You need to lowerCase the rest of the string too and I would test you have a string in the first place
I chose to use + instead of template literals since it is easier to read in this simple case
const captialise = str => str && typeof str === "string" ?
str.slice(0,1).toUpperCase() + str.slice(1).toLowerCase() :
str;
console.log(
captialise(prompt("Enter a string"))
)

Javascript check for a whole word in a unicode string [duplicate]

This question already has answers here:
Find words from array in string, whole words only (with hebrew characters)
(2 answers)
Match any non-word character (excluding diacritics)
(1 answer)
How to ban words with diacritics using a blacklist array and regex?
(5 answers)
Closed 3 years ago.
What is the correct way to check for a whole word in a unicode string in Javascript.
This works for ASCII only:
var strHasWord = function(word, str){
return str.match(new RegExp("\\b" + word + "\\b")) != null;
};
I tried XRegExp as follows but this does not work either.
var strHasWord = function(word, str){
// look for separators/punctuation characters or if the word is the first or the last in the string
var re = XRegExp("(\\p{Z}|\\p{P}|^)" + word + "(\\p{Z}|\\p{P}|$)");
return re.test(str);
//return str.match(re);
}
Any suggestions. Thanks.
EDIT 1
The following seems to do the trick.
var function strHasWord = function(word, str){
var re = RegExp("(\\p{Z}|\\p{P}|^)" + word + "(\\p{Z}|\\p{P}|$)", "u");
return str.match(re) != null;
//return re.test(str);
}

Capitalizing certain strings in an array [duplicate]

This question already has answers here:
Convert string to Title Case with JavaScript
(68 answers)
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
How can I capitalize the first letter of each word in a string using JavaScript?
(46 answers)
Closed 4 years ago.
I basically want to capitalize the first letter in every word in a sentence, assuming that str is all lowercase. So here, I tried to split the string, letter by letter, then by using for loop, I would capitalize whatever the letter that's after a space. Here's my code and could you please point out where I coded wrong? Thank you.
function titleCase(str) {
var strArray = str.split('');
strArray[0].toUpperCase();
for (i=0; i<strArray.length;i++){
if (strArray[i]===" "){
strArray[i+1].toUpperCase();
}
}
return strArray.join('');
}
You need to assign the values:
function titleCase(str) {
var strArray = str.split('');
strArray[0] = strArray[0].toUpperCase();
for (i=0; i<strArray.length;i++){
if (strArray[i]===" "){
strArray[i+1] = strArray[i+1].toUpperCase();
}
}
return strArray.join('');
}
You can try following
function titleCase(str) {
var strArray = str.split(' ');
for (i=0; i<strArray.length;i++){
strArray[i] = strArray[i].charAt(0).toUpperCase() + strArray[i].slice(1);
}
return strArray.join(' ');
}
console.log(titleCase("i am a sentence"));

capitalise first letter - CAN'T Use 'toUpperCase' (JS) [duplicate]

This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 7 years ago.
function to capitalise first letter of a string - 'toUpperCase' , underscore and other jQuery are excluded . I reworked a vers with underscore which I can't use
```
function capitalize (str){
var str = "";
var lowercase = "";
var Uppercase = "";
str.forEach(){
for (i=0; i < str.length; i++);
}
return Uppercase[lowercase.indexOf(str0)];
}
```
There are lots of reduced vers using toUpperCase
Any links, code help pls .... Tks
The best method I've found is just to call toUpperCase on the first character and concat the rest of the string using slice:
function capitalize(str) {
if(typeof str === 'string') {
return str[0].toUpperCase() + str.slice(1);
}
return str;
}
If you want to capitalize each word in a sentence, you can split on space:
"capitalize each word of this sentence".split(' ').map(capitalize).join(' ');

JS Regex: Replace all but first [duplicate]

This question already has an answer here:
use regex to replace all but first occurrence of a substring of blanks
(1 answer)
Closed 9 years ago.
How can I make a Regex which replaces all occurrences of a word but the first?
I have a webpage with loads of text and a header at the top. I want to make a regex which replaces all occurrences of a word but the first because I don't want the header to change.
var count = 0;
text = text.replace(myRegex, function(match) {
count++;
if(count==1) {
return match;
}
else {
return myReplacedValue;
}
});
You could do this:
var i = 0;
"foo foo foo".replace(/foo/g, function(captured/*, offset, originalString */) {
if ( i++ ) {
return 'bar';
}
return captured;
});
great answer abstract... i have never seen replace used like that before
heres a nice cluttered version..
text = text.replace(/boo/g, function(match) {return (++count==1)?match:myReplacedValue});

Categories

Resources