This question already has answers here:
Finding the nth occurrence of a character in a string in javascript
(6 answers)
Closed 9 years ago.
I have a JavaScript variable which contains something like "fld_34_46_name". I need to be able to find the location of the THIRD _. The numbers, and name are not always the same (so, the string might also look like "fld_545425_9075_different name_test").
Is this possible? How could I do it?
Use the indexOf method three times:
var i = s.indexOf('_');
i = s.indexOf('_', i + 1);
i = s.indexOf('_', i + 1);
Note: If the string might contain fewer than three underscores, you would need to check for -1 after each time.
Related
This question already has answers here:
How do I find an exact word in a string?
(5 answers)
Closed 28 days ago.
I want to compare the following two variables to see if they match exactly or not.
What I am doing right now is:
var string1 = "S1";
var string2 = "LS1 B26 M90";
let result = string2.indexOf(string1);
It returns 1 which means S1 exists in string2. I want it to look for "S1" and not to match with "LS1".
you can simply achive by below:
String("LS1 B26 M90").split(" ").includes("LS1")
String("LS1 B26 M90").split(" "): convert string into string list.
.includes("LS1"): will check the existence it return true in case of
match otherwise false.
This question already has answers here:
Javascript nth occurance of a string and extract the sub string upto that
(3 answers)
Regex expression to cut string at nth occurrence of character and return first part of string
(3 answers)
Cutting a string at nth occurrence of a character
(5 answers)
Closed 3 years ago.
I want to get a substring that is before the nth point.
For example I have:
let str = "my.string.is.like.that"
Now suppose I want substr= "my.string.is.like" that is all before the 4th point. How can I do that?
You can use split and join,
Second parameter in split is used to limit the number of element to be included in final output
let str = "my.string.is.like.that"
let limited = str.split('.',4).join('.')
console.log(limited)
This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
Closed 5 years ago.
I need JavaScript algorithm that can match substring of a sting?
subStringFinder('abbcdabbbbbck', 'ab')
should return index 0
and
subStringFinder('abbcdabbbbbck', 'bck') should return index 10
Could you please tell me how to write this code?
--EDIT:
Thanks to #Jonathan.Brink I wrote that code and it did the trick:
function subStringFinder(str, subString) {
return str.indexOf(subString);
}
subStringFinder('abbcdabbbbbck', 'bck') // -> 10
You are looking for the indexOf function which is available via the built-in string type (as well as array).
Example:
var str = "abbcdabbbbbck";
var n = str.indexOf("bck");
// n is 9
Probably, rather than having a custom subStringFinder function it would be better to just use indexOf.
This question already has answers here:
Simple regular expression for a decimal with a precision of 2
(17 answers)
Closed 6 years ago.
What's the best way to extract numbers (with dot in between sometimes)from string in one line?
var str = "ghjkhjgbkhj123.45khgbkhjgk67 8kjhgkj hg13.99sads";
I need an array of [123.45, 67, 8, 13.99];
str.match(/\d+/g) returns slightly different results - it doesn't count ".".
Use regex as /\d+(?:\.\d+)?/g
var str = "ghjkhjgbkhj123.45khgbkhjgk67 8kjhgkj hg13.99sads";
console.log(
str.match(/\d+(?:\.\d+)?/g)
);
Regex explanation here.
str.match(/[.\d]+/g) will do what you are asking for. It will allow for more than one dot, so if you want to do something "sensible" given 123.456.789 you will probably want something more complicated -- but in that case you should first figure out exactly what more sensible thing you want.
This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 8 years ago.
I want to format a number to two decimal places. Say the user enters 8764444 it should be formatted as 8.76. is there some built-in function in javascript to do that?
No, there is no built in method for exactly that, but you can use the substr method to get parts of a string to do the formatting:
var input = "8764444";
input = input.substr(0, 1) + '.' + input.substr(1, 2);
// show result in Stackoverflow snippet
document.write(input);