Monday, 3 June 2013

Making Startswith() and Endswith() in javascript

Hi,

By using prototypes in Javascript we can build our own methods with our own logic.Here in the following code i made two methods for comparing the string with starwith and endwith.
<script>
if ( typeof String.prototype.startsWith != 'function' ) {
  String.prototype.startsWith = function( str ) {
    return this.substring( 0, str.length ) === str;
  }
};

alert( "hello world".startsWith( "hello" ) );

if ( typeof String.prototype.endsWith != 'function' ) {
  String.prototype.endsWith = function( str ) {
    return this.substring( this.length - str.length, this.length ) === str;
  }
};

alert( "hello world".endsWith( "world" ) );
</script>

No comments:

Post a Comment

Best Practices for Building Prompt Templates

 Hi, The following best practices should be considered when building prompt templates: Make sure that your prompt templates are concise and ...