473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Javascript string functions

TNT
48
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?
May 16 '07 #1
4 40353
iam_clint
1,208 Expert 1GB
yes
Expand|Select|Wrap|Line Numbers
  1. var string = "the cat"
  2. string.indexOf("a");
  3.  
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.
Expand|Select|Wrap|Line Numbers
  1. function Left(str, n){
  2.     if (n <= 0)
  3.         return "";
  4.     else if (n > String(str).length)
  5.         return str;
  6.     else
  7.         return String(str).substring(0,n);
  8. }
  9. function Right(str, n){
  10.     if (n <= 0)
  11.        return "";
  12.     else if (n > String(str).length)
  13.        return str;
  14.     else {
  15.        var iLen = String(str).length;
  16.        return String(str).substring(iLen, iLen - n);
  17.     }
  18. }
  19.  
May 16 '07 #2
TNT
48
yes
Expand|Select|Wrap|Line Numbers
  1. var string = "the cat"
  2. string.indexOf("a");
  3.  
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.
Expand|Select|Wrap|Line Numbers
  1. function Left(str, n){
  2. if (n <= 0)
  3. return "";
  4. else if (n > String(str).length)
  5. return str;
  6. else
  7. return String(str).substring(0,n);
  8. }
  9. function Right(str, n){
  10. if (n <= 0)
  11. return "";
  12. else if (n > String(str).length)
  13. return str;
  14. else {
  15. var iLen = String(str).length;
  16. return String(str).substring(iLen, iLen - n);
  17. }
  18. }
  19.  
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"?
May 17 '07 #3
iam_clint
1,208 Expert 1GB
ok so you have these functions
Expand|Select|Wrap|Line Numbers
  1. function Left(str, n){
  2.     if (n <= 0)
  3.      return "";
  4.     else if (n > String(str).length)
  5.      return str;
  6.     else
  7.      return String(str).substring(0,n);
  8. }
  9. function Right(str, n){
  10. if (n <= 0)
  11. return "";
  12. else if (n > String(str).length)
  13. return str;
  14. else {
  15. var iLen = String(str).length;
  16. return String(str).substring(iLen, iLen - n);
  17. }
  18. }
  19.  
now you want the right 4 characters of a string

Expand|Select|Wrap|Line Numbers
  1. var thestring = "this is a simple test";
  2. var somestring  = Right(thestring, 4);
  3. alert(somestring);
  4.  
somestring would = test

Expand|Select|Wrap|Line Numbers
  1. var thestring = "this is a simple test";
  2. var somestring  = Left(thestring, 4);
  3. alert(somestring);
  4.  
somestring would = this
May 17 '07 #4
markhu
2
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.

Expand|Select|Wrap|Line Numbers
  1. x="sample string";
  2.  
  3. r=x.slice(0,4) + ' : ' + x + ' : ' + x.slice(-4);
  4.  
  5. alert( r );
r will be "samp : sample string : ring"
Aug 25 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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...
6
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...
76
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...
17
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.