Handling String in JavaScript
By: aathishankaran in JavaScript Tutorials on 2007-03-29
In JavaScript, the substring()
method is used to extract a portion of a string. It takes two parameters: the starting index and the ending index of the substring. Here's an example:
const str = "Hello, world!"; const subStr = str.substring(0, 5); // "Hello"
In the example above, the substring()
method is used to extract the first five characters of the string.
Formatted strings, also known as template literals, allow for easy string interpolation and formatting. They use the backtick (`) character to define the string and allow for placeholders, which are denoted by a dollar sign followed by curly braces (${expression}). Here's an example:
const name = "Alice"; const age = 25; const formattedString = `My name is ${name} and I am ${age} years old.`; console.log(formattedString); // "My name is Alice and I am 25 years old."
In the example above, the formattedString
variable is defined using a template literal, with placeholders for the name
and age
variables. When the string is logged to the console, the placeholders are replaced with their corresponding values.
Keep in mind the context in which you can use the formatting methods. You cannot use them outside a JavaScript script as a substitute for HTML formatting tags.
String Method | Example Returns | HTML Code |
anchor("anchorName") |
"Section3".anchor |
<A NAME="Section3">Section('Section3")3</A> |
big() |
"Just save the name".big() |
<BIG>Just save the name</BIG> |
Blink() |
"Why ask why".blink() |
<BLlNK>Why ask why</BLlNK> |
Boid() |
"Liveandlearn".bold() |
<B>Liveandlearn</B> |
Fixed() |
"JavaScriptcode".fixed() |
<TT>JavaScript code</TT> |
fontcolor("color") |
"JavaPrograms".fontcolor("green") |
<FONTCOLOR"green">JavaPrgrams</FONT> |
fontsize(size) |
"Impact".fontsize(3) |
<FONT SIZE="3">lmpact</FONT> |
Italics() |
"Java Script Unleashed".italics() |
<I>JavaScript Unleashed</I> |
Link("URL") |
"See My Home Page". link("http://www.myhomepage.com/") |
<A HREF="http//www.myhomepage.com/> SeeMyHomePage</A> |
Small() |
"TinyTim".small() |
<SMALL>Tiny Tim</SMALL> |
Strike() |
"GameCanceled"strike() |
<STRIKE>GameCanceled</STRIKE> |
Sub() |
"Submarines".sub() |
<SUB>Submarines</SUB> |
sup() |
"Superman".sup() |
<SUP>Superman</SUP> |
toLowerCase() |
"SeeSPOTrUN".toLowerCase() |
seespotrun |
ToUpperCase() |
"let's go to the BEACH".toUpperCase() |
LET'S GO TO THE BEACH |
Working with Special Characters
When working with strings in any language, you will discover certain characters that are difficult to use. JavaScript enables you to work with these special case characters by using a backslash character (\) followed by the character or its code. Table below lists the JavaScript in line symbols.
Symbol |
Description |
\t |
Tab |
\n |
New line |
\r |
Carriage return |
\f |
Form feed |
\\ |
Backslash |
\b |
Backspace |
\" |
Double quote |
\' |
Single quote |
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments