tag:blogger.com,1999:blog-865923359735383241.post8372438954294788411..comments2023-10-29T07:27:09.012-06:00Comments on Ccna final exam - java, php, javascript, ios, cshap all in one: Javascript StartsWithUnknownnoreply@blogger.comBlogger9125tag:blogger.com,1999:blog-865923359735383241.post-35821024025921566662012-06-01T22:38:18.423-06:002012-06-01T22:38:18.423-06:00var str = 'hol';
var data = 'hola mund...var str = 'hol';<br />var data = 'hola mundo';<br />if (data.length >= str.lenght && data.substring(0, str.length) == str)<br /> return true;<br />else<br /> return false;Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-18663192020318739112012-06-01T22:38:17.422-06:002012-06-01T22:38:17.422-06:00var str = 'he';
var data = 'hello wor...var str = 'he';<br /><br />var data = 'hello world';<br /><br />String.prototype.startsWith = function(s)<br />{<br /> if( this.indexOf(s) == 0 ) return true;<br /> return false;<br />}<br /><br />if( data.startsWith(str) ) return true;Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-54077674286446402602012-06-01T22:38:16.209-06:002012-06-01T22:38:16.209-06:00Also check out underscore.string.js. It comes with...Also check out underscore.string.js. It comes with a bunch of useful string testing and manipulation methods.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-78119669313473278592012-06-01T22:38:14.733-06:002012-06-01T22:38:14.733-06:00Without the use of a helper function:
(/^He/).tes...Without the use of a helper function:<br /><br />(/^He/).test('Hello world')Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-72725411019333873902012-06-01T22:38:13.860-06:002012-06-01T22:38:13.860-06:00Here's a related JS Perf. Looks like lastIndex...Here's a related JS Perf. Looks like lastIndexOf is a good cross-browser choice.<br /><br />http://jsperf.com/js-startswith/6Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-18288309762072532152012-06-01T22:38:13.209-06:002012-06-01T22:38:13.209-06:00Here is a minor improvement to CMS's solution:...Here is a minor improvement to CMS's solution:<br /><br />if(!String.prototype.startsWith){<br /> String.prototype.startsWith = function (str) {<br /> return !this.indexOf(str);<br /> }<br />}<br /><br />"Hello World!".startsWith("He"); // true<br /><br /> var data = "Hello world";<br /> var input = 'He';<br /> data.startsWith(input); // true<br /><br /><br />Checking whether the function already exists in case a future browser implements it in native code or if it is implemented by another library. For example, the Prototype Library implements this function already.<br /><br />Using ! is slightly faster and more concise than " === 0" though not as readable.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-86503336126053457542012-06-01T22:38:12.513-06:002012-06-01T22:38:12.513-06:00Here's another alternative:
data.lastIndexOf(...Here's another alternative:<br /><br />data.lastIndexOf(str, 0) === 0<br /><br /><br /><br />It doesn't check the entire string.<br />It doesn't create a new temporary string and then immediately discard it.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-50343758723385957612012-06-01T22:38:11.499-06:002012-06-01T22:38:11.499-06:00data.substring(0, input.length) === inputdata.substring(0, input.length) === inputUserhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-69488398272535222412012-06-01T22:38:04.583-06:002012-06-01T22:38:04.583-06:00You can add this function to the String prototype:...You can add this function to the String prototype:<br /><br />if (typeof String.prototype.startsWith != 'function') {<br /> String.prototype.startsWith = function (str){<br /> return this.indexOf(str) == 0;<br /> };<br />}<br /><br /><br />Then you can use it directly on string values:<br /><br />"Hello World!".startsWith("He"); // true<br /><br />var data = "Hello world";<br />var input = 'He';<br />data.startsWith(input); // true<br /><br /><br />Edit: Note that I'm checking if the function exists before defining it, that's because in the future, the language might have this strings extras methods defined as built-in functions, and native implementations are always faster and preferred, see the ECMAScript Harmony String Extras proposal.<br /><br />Edit: As others noted, indexOf will be inefficient for large strings, its complexity is O(N). For a constant-time solution (O(1)), you can use either, substring as @cobbal suggested, or String.prototype.slice, which behaves similarly (note that I don't recommend using the substr, because it's inconsistent between implementations (most notably on JScript) ):<br /><br />if (typeof String.prototype.startsWith != 'function') {<br /> String.prototype.startsWith = function (str){<br /> return this.slice(0, str.length) == str;<br /> };<br />}<br /><br /><br />The difference between substring and slice is basically that slice can take negative indexes, to manipulate characters from the end of the string, for example you could write the counterpart endsWith method by:<br /><br />if (typeof String.prototype.endsWith != 'function') {<br /> String.prototype.endsWith = function (str){<br /> return this.slice(-str.length) == str;<br /> };<br />}Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.com