I have two questions concerning Javascript
1: Is there a way to search for a character in a string?
2: Is there a way to "chop off" some characters from the start of a string?
4 40353
yes -
var string = "the cat"
-
string.indexOf("a");
-
will return 5 because the a is the 5th character
You can read this Article on string functionality.
and yes you can chop off characters.
heres some left and right functions you may be used to using in other languages. -
function Left(str, n){
-
if (n <= 0)
-
return "";
-
else if (n > String(str).length)
-
return str;
-
else
-
return String(str).substring(0,n);
-
}
-
function Right(str, n){
-
if (n <= 0)
-
return "";
-
else if (n > String(str).length)
-
return str;
-
else {
-
var iLen = String(str).length;
-
return String(str).substring(iLen, iLen - n);
-
}
-
}
-
yes -
var string = "the cat"
-
string.indexOf("a");
-
will return 5 because the a is the 5th character
You can read this Article on string functionality.
and yes you can chop off characters.
heres some left and right functions you may be used to using in other languages. -
function Left(str, n){
-
if (n <= 0)
-
return "";
-
else if (n > String(str).length)
-
return str;
-
else
-
return String(str).substring(0,n);
-
}
-
function Right(str, n){
-
if (n <= 0)
-
return "";
-
else if (n > String(str).length)
-
return str;
-
else {
-
var iLen = String(str).length;
-
return String(str).substring(iLen, iLen - n);
-
}
-
}
-
I am new to Javascript (only reading a book on the basics four days ago)
Can you post examples instead of the "str" and "n"?
ok so you have these functions -
function Left(str, n){
-
if (n <= 0)
-
return "";
-
else if (n > String(str).length)
-
return str;
-
else
-
return String(str).substring(0,n);
-
}
-
function Right(str, n){
-
if (n <= 0)
-
return "";
-
else if (n > String(str).length)
-
return str;
-
else {
-
var iLen = String(str).length;
-
return String(str).substring(iLen, iLen - n);
-
}
-
}
-
now you want the right 4 characters of a string -
var thestring = "this is a simple test";
-
var somestring = Right(thestring, 4);
-
alert(somestring);
-
somestring would = test -
var thestring = "this is a simple test";
-
var somestring = Left(thestring, 4);
-
alert(somestring);
-
somestring would = this
There is no leftstr() or rightstr() functions in JavaScript because the slice() function handles both.
Here is an example that shows the first 4 chars of a string, then the last 4. - x="sample string";
-
-
r=x.slice(0,4) + ' : ' + x + ' : ' + x.slice(-4);
-
-
alert( r );
r will be "samp : sample string : ring"
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sue |
last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided
to improve character checking on my project. In my project I only had
to do very basic validation. Therefore, I only had one...
|
by: Andy Fish |
last post by:
Hi,
I want to use an anchor tag to invoke some javascript and I've read that
it's bad form to use <a href="javascript:foo()">
I've read endless usenet posts and hint sites on the net, they all...
|
by: lorlarz |
last post by:
Crockford's JavaScript, The Good Parts (a book review).
This shall perhaps be the world's shortest book review (for one of the
world's
shortests books).
I like Douglas Crockford (because I am a...
|
by: yawnmoth |
last post by:
http://www.frostjedi.com/terra/scripts/demo/this-alert.html
http://www.frostjedi.com/terra/scripts/demo/this-alert2.html
Why, when you click in the black box, do the alert boxes say different...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |