By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,471 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

String Manipulations through Javascript

P: 17
Title: String Manipulations through Javascript
Author: Vivek Kapile
Email: snipped
Language: JavaScript
Platform: Javascript
Technology: JavaScript
Level: Beginner

Introduction
This code will help for the beginners, who want to learn, how to manage a substring through Javascript.
for ex. @Right("Vivek", "i") returns "vek"


Description:

Below is a sample code.

Expand|Select|Wrap|Line Numbers
  1. function rightString(fullString, subString) {
  2.    if (fullString.indexOf(subString) == -1) {
  3.       return "";
  4.    } else {
  5.       return (fullString.substring(fullString.indexOf(subString)+subString.length, fullString.length));
  6.    }
  7. }
  8.  
  9.  
  10. function rightBackString(fullString, subString) {
  11.    if (fullString.lastIndexOf(subString) == -1) {
  12.       return "";
  13.    } else {
  14.       return fullString.substring(fullString.lastIndexOf(subString)+1, fullString.length);
  15.    }
  16. }
  17.  
  18.  
  19. function middleString(fullString, startString, endString) {
  20.    if (fullString.indexOf(startString) == -1) {
  21.       return "";
  22.    } else {
  23.       var sub = fullString.substring(fullString.indexOf(startString)+startString.length, fullString.length);
  24.       if (sub.indexOf(endString) == -1) {
  25.          return sub;
  26.       } else {
  27.          return (sub.substring(0, sub.indexOf(endString)));
  28.       }
  29.    }
  30. }
  31.  
  32.  
  33. function middleBackString(fullString, startString, endString) {
  34.    if (fullString.lastIndexOf(startString) == -1) {
  35.       return "";
  36.    } else {
  37.       var sub = fullString.substring(0, fullString.lastIndexOf(startString));
  38.       if (sub.indexOf(endString) == -1) {
  39.          return sub;
  40.       } else {
  41.          return (sub.substring(sub.indexOf(endString)+endString.length, sub.length));
  42.       }
  43.    }
  44. }
  45.  
  46.  
  47. function leftString(fullString, subString) {
  48.    if (fullString.indexOf(subString) == -1) {
  49.       return "";
  50.    } else {
  51.       return (fullString.substring(0, fullString.indexOf(subString)));
  52.    }
  53. }
  54.  
  55.  
  56. function leftBackString(fullString, subString) {
  57.    if (fullString.lastIndexOf(subString) == -1) {
  58.       return "";
  59.    } else {
  60.       return fullString.substring(0, fullString.lastIndexOf(subString));
  61.    }
  62. }
Summary

There are many topics to learn with Javascript. I have covered a small portion hope this will helps all the beginners to start up. Please give your feedback and suggestion.

History

* Written on 17-March-2011, Thursday


References

License

This article, along with any associated source code and files, is licensed under The Bytes

About the Author

Vivek kapile
Developer
India,Bangalore
Mar 17 '11 #1
Share this Article
Share on Google+