0

I need a function to make a string camelcase using only javascript. I want this without builtin functions like split and all. Please include an explanation.

If I have "dean john" I want it to be changed to "Dean John".

var myString = "Hello world!";
var myArray = [];
var out= ""
for (var i=0; i < myString.length; i++){
    myArray.push(myString[i]);
    //myArray.pop(myString[i]);
    myArray[0].toUpperCase();
}
alert(myArray)

Desired output:

Hello World

I don't want to use split or any other builtin function.

0x860111
  • 386
  • 1
  • 3
  • 15
  • 2
    You don't want _camelCase_ here, you want to uppercase the first letter of every word. – Tushar Sep 20 '15 at 07:42
  • yes exacly but how can i detect space in arrray – Deepak Sabharwal Sep 20 '15 at 07:44
  • 1
    possible duplicate of [Javascript - How to capitalize first letter of each word, like a 2-word city?](http://stackoverflow.com/questions/4878756/javascript-how-to-capitalize-first-letter-of-each-word-like-a-2-word-city) – Kristijan Iliev Sep 20 '15 at 07:48
  • possible duplicate of [Convert string to title case with javascript](http://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript) – Grundy Sep 20 '15 at 08:36
  • @Tushar I guess your refer to "lower Camel Case", which is often what one means when talking about programming ... https://en.wikipedia.org/wiki/CamelCase – Asons Sep 20 '15 at 08:49

4 Answers4

2

Use replace with regex:

str.replace(/(^ *| +)(.)/g, function(v,p1,p2){return p1+p2.toUpperCase()})

Using for loop you can do it this way:

var u=true, r=''
for(var i=0; i < str.length; i++){
  c=str[i]
  r+=u?c.toUpperCase():c
  u=(c==' ')
}
Pavel Gatnar
  • 3,987
  • 2
  • 19
  • 29
1

Yet another way, use boundaries: \b

\b Matches a zero-width word boundary, such as between a letter and a space. (Not to be confused with [\b])

For example, /\bno/ matches the "no" in "at noon"; /ly\b/ matches the "ly" in "possibly yesterday".

so regex can be like this

myString.replace(/\b(\w)/g,function(m,p1){ return p1.toUpperCase();})
Grundy
  • 13,356
  • 3
  • 35
  • 55
1

If you will show it as HTML you can use CSS

.capitalize-all-words {
    text-transform: capitalize;
}

in your view

<span class='capitalize-all-words'>hello world</span>
Asons
  • 84,923
  • 12
  • 110
  • 165
0

Hope this helps you !!

String.prototype.toTitleCase = function(){
  var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;

  return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
    if (index > 0 && index + match.length !== title.length &&
      match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
      (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
      title.charAt(index - 1).search(/[^\s-]/) < 0) {
      return match.toLowerCase();
    }

    if (match.substr(1).search(/[A-Z]|\../) > -1) {
      return match;
    }

    return match.charAt(0).toUpperCase() + match.substr(1);
  });
};

document.getElementById("rawText").innerHTML = document.getElementById("rawText").innerHTML.toTitleCase();
<div id="rawText">sdfdsfsd sdfsdf awwads</div>
Kishore Sahasranaman
  • 4,013
  • 3
  • 24
  • 50