Add a space between characters in a String [duplicate] - javascript

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);

Related

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>

How to remove the character form string using jQuery? [duplicate]

This question already has answers here:
Regex to remove letters, symbols except numbers
(6 answers)
Strip all non-numeric characters from string in JavaScript
(12 answers)
Closed 6 years ago.
How to remove character from strings (a-z, A to Z) using jQuery or JavaScript?
if str=avc234jw6;
I need only 2346.
A simple String.replace with regex /[a-z]/ig can do!
var str = "avc234jw6";
var no = parseInt(str.replace(/[a-z]/ig, ""));
console.log(no);
Answer on non-edited question "How to remove char from strin?"
var removeChar = "a";
var string = "abc abc abc";
function removeFromString(char, string){
var newString = "";
for (var i = 0; i < string.length; i++) {
if(string[i].toLowerCase() == char){
continue;
} else {
newString += string[i];
}
}
return newString;
}
$("#test").html(removeFromString(removeChar, string));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test">
test
</div>

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("");

Remove a character at a certain position in a string - javascript [duplicate]

This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
Is there an easy way to remove the character at a certain position in javascript?
e.g. if I have the string "Hello World", can I remove the character at position 3?
the result I would be looking for would the following:
"Helo World"
This question isn't a duplicate of How can I remove a character from a string using JavaScript?, because this one is about removing the character at a specific position, and that question is about removing all instances of a character.
It depends how easy you find the following, which uses simple String methods (in this case slice()).
var str = "Hello World";
str = str.slice(0, 3) + str.slice(4);
console.log(str)
You can try it this way:
var str = "Hello World";
var position = 6; // its 1 based
var newStr = str.substring(0, position - 1) + str.substring(position, str.length);
alert(newStr);
Here is a live example: http://jsbin.com/ogagaq
Turn the string into array, cut a character at specified index and turn back to string
let str = 'Hello World'.split('')
str.splice(3, 1)
str = str.join('')
// str = 'Helo World'.
If you omit the particular index character then use this method
function removeByIndex(str,index) {
return str.slice(0,index) + str.slice(index+1);
}
var str = "Hello world", index=3;
console.log(removeByIndex(str,index));
// Output: "Helo world"
var str = 'Hello World';
str = setCharAt(str, 3, '');
alert(str);
function setCharAt(str, index, chr)
{
if (index > str.length - 1) return str;
return str.substr(0, index) + chr + str.substr(index + 1);
}
you can use substring() method. ex,
var x = "Hello world"
var x = x.substring(0, i) + 'h' + x.substring(i+1);
Hi starbeamrainbowlabs ,
You can do this with the following:
var oldValue = "pic quality, hello" ;
var newValue = "hello";
var oldValueLength = oldValue.length ;
var newValueLength = newValue.length ;
var from = oldValue.search(newValue) ;
var to = from + newValueLength ;
var nes = oldValue.substr(0,from) + oldValue.substr(to,oldValueLength);
console.log(nes);
I tested this in my javascript console so you can also check this out
Thanks
var str = 'Hello World',
i = 3,
result = str.substr(0, i-1)+str.substring(i);
alert(result);
Value of i should not be less then 1.

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

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

Categories

Resources