This question already has answers here:
How to use split?
(4 answers)
Closed 7 years ago.
I have a string like this 132+456 or 132-456 or 132*456..etc ,it changes dynamically but I need to split this into 132 and 456 how to do it using pure java script?
It should be as easy as:
var parts = '132+456'.split('+');
parts[0]; //132
parts[1]; //456
Like so
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];
// document.writeln only for example
document.writeln(a);
document.writeln(b);
Java
String[] values='132+456'.split('+');
String firstPart=value[0]; // has 132
for js
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];
Related
This question already has answers here:
JavaScript equivalent to printf/String.Format
(59 answers)
Closed 1 year ago.
I have a string and array
var s1 = "hello %s, i am %d years old";
var s2 =[John,24];
Expected result:
s3 = hello John i am 24 years old
I want to save the output into another string.
I'm able to display output in console
console.log(s1, ...s2)
But not able to store in other string.
I tried many things like:
var s3 = s1.format(...s2)
Any suggestions?
Unfortunately there is no string formatter available in JS, you'd have to write that manually like
let i = 0;
s3 = s1.replace(/%(s|d)/g, (_, type) => s2[i++]);
You could use the template string from ES6:
let anotherString = `Hello ${s2[0]}, I am ${s2[1]} years old`;
You could use this too:
String.prototype.format = function() {
a = this;
for (k in arguments) {
a = a.replace("{" + k + "}", arguments[k])
}
return a
}
Usage:
let anotherString = '{0} {1}!'.format('Hello', 'Word');
Than those solutions I dont see any other way to do that.
This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
How to force addition instead of concatenation in javascript [duplicate]
(3 answers)
Closed 3 years ago.
I have the following error:
jquery-3.4.1.min.js:2 The specified value "24.164.83" is not a valid
number. The value must match to the following regular expression:
-?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)?
Code
var grossTotal = netPrice * vatTotal // Is OK - it multiplies values
but
var grossTotal = netPrice + vatTotal // It makes this error -
it doesn’t sum.
The easiest way to produce a number from a string is prepend with +
var grossTotal= +netPrice + +vatTotal;
Try this code:
var netPrice = 1.432;
var vatTotal = 14.423;
var grossTotal = parseFloat(netPrice) + parseFloat(vatTotal)// Change according to data type of netPrice and/or vatTotal
console.log(grossTotal);
Try with type conversion:
var answer = parseInt(netPrice ) + parseInt(vatTotal);
You can use this code, First format value using parseFloat
var netPrice = 2.3777;
var vatTotal = 1.3777;
var grossTotal = parseFloat(netPrice ) + parseFloat(vatTotal);
console.log(grossTotal);
var netPrice = 2;
var vatTotal = 1;
var grossTotal = parseInt(netPrice ) + parseInt(vatTotal);
console.log(grossTotal);
For more information about parseFloat
https://www.w3schools.com/jsref/jsref_parsefloat.asp
This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
I have data like
var result = 'Zika NAA, Blood|Zika NAA, Urine'
My requirement is
var result = Zika NAA, Blood
Zika NAA, Urine
| symbol shouldn't be there. How to achieve in javascript ?
document.write('Zika NAA, Blood|Zika NAA, Urine'.split('|').join('<br>'));
var result = 'Zika NAA, Blood|Zika NAA, Urine'.replace('|', ' ');
console.log(result);
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I have write following code to split the URL
var href = document.URL;
var GlobalBoomGuid = href.split("=");
var optionid = GlobalBoomGuid[1];
If i have URL like this : www.mydomain.com?optionid=655 It's giving me "655" .
But in current scenario my URL is www.mydomain.com?optionid=655#&ui-state=dialog and it's returning me 655#&ui-state
Now i want only id . How should i do that ?
Please if you dont like the question don't mark as a Negative
Thanx in Advance :)
This gets your result
var href = document.URL;
var foo = href.split("=")[1];
var GlobalBoomGuid = foo.split("#")[0];
var optionid = GlobalBoomGuid;
fiddle
This question already has answers here:
Is there a JavaScript function that can pad a string to get to a determined length?
(43 answers)
Closed 8 years ago.
I know this is a gimme, but I'm trying to make the filenames serialized with four digits instead of one. This function is for exporting PNG files from layers within Adobe Illustrator. Let me know if you ever need icons - much respect.
var n = document.layers.length;
hideAllLayers ();
for(var i=n-1, k=0; i>=0; i--, k++)
{
//hideAllLayers();
var layer = document.layers[i];
layer.visible = true;
var file = new File(folder.fsName + '/' +filename+ '-' + k +".png");
document.exportFile(file,ExportType.PNG24,options);
layer.visible = false;
}
Use util.printf (see the Acrobat API, page 720):
var file = new File(util.printf("%s/%s-%04d.png", folder.fsName, filename, k));
You can pad your number to the left and take the last four characters like this:
var i = 9;
var num = ("0000"+i);
var str = "filename"+(num.substring(num.length-4)); //filename0009
Or shorter
str = ("0000" + i).slice(-4)
Thanks to this question