Do we have something like C# String.Format(...) in JavaScript? [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript equivalent to printf/string.format
Do we have something like C# String.Format(...) in JavaScript?
I like to be able to say String.Format('text text {0}, text text {1}', value1, value2);
and ideally as an extension method:
'text text {0}, text text {1}'.format(value1, value2);
Thanks,

here is your solution:
String.format = function() {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i + 1]);
}
return s;
}
more informations here => Equivalent of String.format in jQuery

Related

convert javascript string to regular expression [duplicate]

This question already has answers here:
Match #(\w+) and replace in javascript
(2 answers)
Closed 6 years ago.
I have the following
function arrayInArray(chBox, values) {
try {
for (var y = 0; y < chBox.length; y++) {
var stringConcat = "/\b" + chBox[x] + ",?\b/";
var patt = new RegExp(eval(stringConcat));
console.log("value to check: " + chBox[y] + " " + values + " index " + patt.test(values));
but I can't get it to work. if I change it to this
var patt = new RegExp(/\b17,?\b/)
and then run
patt.test(values)
it works fine.
I'm passing in
var checkbox = ["17", "23"];
Can anyone tell me where I am going wrong
thanks
the code that works
for (var y = 0; y < chBox.length; y++) {
var stringConcat = "\\b" + chBox[y] + ",?\\b";
var patt = new RegExp(stringConcat);
if (patt.test(values) == false) return false;
}
There's no need for eval as its already a string. Just pass that to the RegExp constructor. You should also drop the leading and trailing / as that will be interpreted as part of the expression.

Javascript replace string by another String [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 4 years ago.
I have following string in javascript.
var str = 'P24 + P33'; //p24 is just exp. it will be any number i.e. P98
I Want to replace this string into following string using jquery replace.
var str = "$('#p24').val() + $('#p33').val()";
var str = 'P24 + P33'; //p24 is just exp. it will be any number i.e. P98
var str_array = str.split(" + ");
console.log("Original string: "+str);
for(var i = 0; i < str_array.length; i++){
str_array[i] = $("#"+str_array[i]).html();
}
var replaced_string = str_array.join(" + ");
console.log("Replaced string: "+replaced_string);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="P24">this is P24</div>
<div id="P33">this is P33</div>

Javascript to java code not working [duplicate]

This question already has answers here:
Double cannot be dereferenced?
(3 answers)
Closed 7 years ago.
Hi so i am trying to convert this JavaScript code to Java i can't seem to be able to do it that good am stuck in the last part i need to convert my JavaScript code to java the encrypt part.
JavaScript Code:
Encrypt = function (strIn) {
var strOut = String();
var lenIn = strIn.length;
var i = 0;
var numRand;
while (i < lenIn) {
numRand = Math.floor(Math.random() * 66) + 36;
strOut += (strIn.charCodeAt(i) + numRand).toString(27) + numRand.toString(27);
i++;
}
return strOut;
};
Java Code:
public static String Encrypt(String strIn) {
String strOut = null;
int lenIn = strIn.length();
int i = 0;
double numRand;
while (i < lenIn) {
numRand = Math.floor(Math.random() * 66) + 36;
strOut += (strIn.charAt(i) + numRand).toString(27) + numRand.toString(27);
i++;
}
return strOut;
}
Am having an error in this line of code:
strOut += (strIn.charAt(i) + numRand).toString(27) + numRand.toString(27);
The Error:
Error:(22, 50) java: double cannot be dereferenced
I can't seem to be able to tell why any help is wonderful.
Thank you very much.
The issue you are running into that double is a primitive type (along with char and int) and so it doesn't have a toString() method (which in Java belongs to Object). To dig deeper, the reason your error message complains about a double is because your numRand variable is of type double, and so due to casting strIn.charAt(i) + numRand also becomes a double.
Instead you can use something like Integer.toString(value, radix), which is a static member of Integer. The revised code might look like...
strOut += Integer.toString((int)(strIn.charAt(i) + (char)numRand), 27) +
Integer.toString((int)numRand, 27);

Java Script - Extract number from string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How might I extract the number from a number + unit of measure string using JavaScript?
How to extract number from string like this in JS.
String: "Some_text_123_text" -> 123
JSFiddle Demo
var s = "Some_text_123_text";
var index = s.match(/\d+/);
document.writeln(index);​
try this
var string = "Some_text_123_text";
var find = string.split("_");
for(var i = 0; i < find.length ; i ++){
if(!isNaN(Number(find[i]))){
var num = find[i];
}
}
alert(num);
try this working fiddle
var str = "Some_text_123_text";
var patt1 = /[0-9]/g;
var arr= str.match(patt1);
var myval = arr.join("");

Add a space between characters in a String [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
String Manipulation - Javascript -
I have a string:
hello
and I want to add a space between each character to give:
h e l l o
What is the best way to do this?
"hello".split('').join(' '); // "h e l l o"
var text = "hello";
var betweenChars = ' '; // a space
alert(text.split('').join(betweenChars));
try:
var hello = 'hello';
var test = '';
for(var i=0; i<hello.length; i++){
test += hello.charAt(i) + ' ';
}
alert(test);

Categories

Resources