Click to See Complete Forum and Search --> : A blank space question
Webskater
01-23-2003, 03:31 PM
I want to make the first letter of every word in a string UpperCase.
So I loop through the string using charAt(i) looking for blank spaces.
How do you say:
if (charAt(i) == " ")
Actually the above works but I don't trust it. I would like to know what I should be putting between the quotation marks to find a space. Thanks for any help.
gil davis
01-23-2003, 03:51 PM
The easiest way to do it would be to use string.split(" "). This returns an array of strings from another string. See http://developer.netscape.com/docs/manuals/js/client/jsref/string.htm#1194452 for more information. Then you just step through the array and capitalize the first letter of each array value.
Dan Drillich
01-23-2003, 04:00 PM
One way to do it is to refer to the character via its hexadecimal number. In our case 20.
Like -
st.charAt(1) == "\x20"
You can also refer to the character via its Unicode number -
st.charAt(1) == "\u0020"
Cheers,
Dan
Webskater
01-23-2003, 04:07 PM
Thank you both for your answers.
Dan, do you know where I can find a list of all the characters and their hex and unicode values?
Also, is hex or unicode preferable? Are there any platform issues?
Maybe this helps you:
<input type=button value=" t est " onClick="alert(this.value.charCodeAt(0))"></input>
charCodeAt(0) is the latin-1 for the space
Charles
01-23-2003, 05:08 PM
Originally posted by Webskater
Thank you both for your answers.
Dan, do you know where I can find a list of all the characters and their hex and unicode values?
Also, is hex or unicode preferable? Are there any platform issues? You can find all of the unicode characters at http://www.unicode.org/. But you really do not want to go that way. You want to use gil davis' method or something like it.
<script type="text/javascript">
<!--
String.prototype.toInitialUpperCase = function () {
var a = this.split('');
a[0] = a[0].toUpperCase();
return a.join('');
}
String.prototype.toTitle = function () {
var a = this.split(/\b/);
for (j=0;j<a.length;j++) {a[j] = a[j].toInitialUpperCase()};
return a.join('');
}
alert('Phiv hatecra, scrar muvali pa depivius imedr.'.toTitle())
// -->
</script>
Webskater
01-24-2003, 03:06 AM
I have to ask - it's forty years since I studied Latin - what does:
'Phiv hatecra, scrar muvali pa depivius imedr'
mean?
Charles
01-24-2003, 04:53 AM
I don't know Latin either; Koine Greek's more my thing. I often use HTML kit to compose my postings and that line was generated by the cmRandomContent plugin. (http://www.chamisplace.com/asp/hkp.asp?f=cmrandomcontent)
Webskater
01-24-2003, 05:45 PM
[QUOTE]
String.prototype.toInitialUpperCase = function () {
var a = this.split('');
a[0] = a[0].toUpperCase();
return a.join('');
}
String.prototype.toTitle = function () {
var a = this.split(/\b/);
for (j=0;j<a.length;j++) {a[j] = a[j].toInitialUpperCase()};
return a.join('');
[QUOTE]
Although the above works perfectly there's a couple of things I don't understand.
I have always written functions thus:
function changetext()
What is the line:
String.prototype.toInitialUpperCase = function ()
actually doing?
Is toInitialUpperCase a javascript function? I know toUpperCase is.
I realise that in the toTitle function a[j] is being passed to the toInitialUpperCase function in the place of "String.prototype". What does "String.prototype" mean?
Even with my lack of understanding it works like a charm. However, you mentioned in your reply that, in regards to looping through a string looking for spaces (or the unicode character for a space), you "don't want to go there". I'm curious to know your thinking on that.
Thanks for your help.
Charles
01-24-2003, 07:34 PM
JavaScript is an object oriented language, though it has some short cuts that allow programmers treat it otherwise. An object is complex data type that can hold any number of other pieces of data. And these other pieces of data can be variables, functions or other objects. Except that functions of an object are known as methods and variables known as properties. And the two are interchangable. You can assign a method to a property. That's why you need the parentheses to call a method. Pretty much all objects are properties of other objects, forming a tree structure not unlike the directory on your hard drive and with window objects as the roots.
JavaScript also has some primitive data types, like arrays and strings but they get converted to Array and String objects as needed. Note the capitalization convention. Class names start with capital letters. Each particular String object is a member of the String class and it inherits methods of that class. Consider
'Enegon pisiscrotis, acequor cisit ra alefore pletoher.'.toUpperCase();
That creates a String object and it calls one of its methods, passing no parameters to it. Now we can give this object it's own method:
s = new String('Enegon pisiscrotis, acequor cisit ra alefore pletoher.');
s.reverse = function () {return this.split('').reverse().join('')};
alert(s.reverse());
Or we can give the entire class the method:
String.protype.reverse = function () {return this.split('').reverse().join('')};
alert('Enegon pisiscrotis, acequor cisit ra alefore pletoher.'.reverse());
We could also have given the String class a method that isn't inherited by the members of that class:
String.reverse = function () {return 'esrever'};
alert(String.reverse());
The construction:
function reverse () {}
is shorthand for
window.reverse();
Now, the String class defines a number of useful methods, among them toUpperCase. It makes sense, in a twisted way perhaps, to have our toTitle method work the same way. We really don't want to make a method of a window object that takes a string as a parameter when we can simply make a method that all Strings will inherit.
Webskater
01-25-2003, 04:40 AM
Charles - thank you for your explanation.