For example, if I had “scissors” in variable and wanted to know the position of all occurrences of the letter “s”, it should print out 1, 4, 5, 8. How can I do this in JavaScript in most efficient way? I don’t think looping through the whole is terribly efficient Answer A simple loop works well: Now, you indicate that
Tag: string
Parse string in javascript
How can I parse this string on a javascript, var string = “http://www.facebook.com/photo.php?fbid=322916384419110&set=a.265956512115091.68575.100001022542275&type=1”; I just want to get the “265956512115091” on the string. I somehow parse this string but, still not enough to get what I wanted. my code: returns: Answer http://jsbin.com/anuhog/edit#source
How to trim a string to N chars in Javascript?
How can I, using Javascript, make a function that will trim string passed as argument, to a specified length, also passed as argument. For example: Anyone got ideas? I’ve heard something about using substring, but didn’t quite understand. Answer Why not just use substring… string.substring(0, 7); The first argument (0) is the starting point. The second argument (7) is the
Split large string in n-size chunks in JavaScript
I would like to split a very large string (let’s say, 10,000 characters) into N-size chunks. What would be the best way in terms of performance to do this? For instance: “1234567890” split by 2 would become [“12”, “34”, “56”, “78”, “90”]. Would something like this be possible using String.prototype.match and if so, would that be the best way to
Return string without trailing slash
I have two variables: I want to do something like this How do I do this? Answer Try this:
Prepend text to beginning of string
What is the fastest method, to add a new value at the beginning of a string? Answer Wouldn’t this work for you?
Add characters to a string in Javascript
I need to add in a For Loop characters to an empty string. I know that you can use the function concat in Javascript to do concats with strings But it doesn’t work with my example. Any idea of how to do it in another way? My code : Answer
How to check if a string contains text from an array of substrings in JavaScript?
Pretty straight forward. In javascript, I need to check if a string contains any substrings held in an array. Answer There’s nothing built-in that will do that for you, you’ll have to write a function for it, although it can be just a callback to the some array method. Two approaches for you: Array some method Regular expression Array some
Convert string to datetime
How to convert string like ’01-01-1970 00:03:44′ to datetime? Answer For this format (assuming datepart has the format dd-mm-yyyy) in plain javascript use dateString2Date. It may bite you, because of browser compatibility problems. tryParseDateFromString is ES6 utility method to parse a date string using a format string parameter (format) to inform the method about the position of date/month/year in the
How to convert a String to long in javascript?
I have a millisecond timestamp that I need to convert from a string to long. JavaScript has a parseInt but not a parseLong. So how can I do this? To expand on my question slightly: given that apparently JavaScript doesn’t have a long type, how can I do simple arithmetic with longs that are initially expressed as strings? E.g subtract