This question already has answers here:
How to add spaces between every character in a string?
(4 answers)
Closed 3 years ago.
How can i convert a text like Test to T e s t I know It can probably be done with regex but i don't understand how
I've, Quite new to javascript, in python (Which i quite understand) It can be done with a for loop something like
print(" ".join(a for a in "Test"))
But join works differently in javascript and only works for arrays (lists) if i'm right
I've also tried using replace but it does nothing
console.log("Test".replace(""," "))
console.log("Test".replace(""," "))
"Test".split("").join(" ")
// or
[..."Test"].join(" ")
Thats it. You can't do that with .join directly as that only accepts a string.
JS doesn't support generator expressions, and join is an array method that takes the joiner not a string method that takes an iterable. The closest equivalent to your Python code would be
console.log(Array.from("Test").join(" "))
Using Array.from (converting the iterable string to an array) over .split("") has the advantage that it doesn't break unicode characters that consist of multiple code points apart.
Related
This question already has answers here:
Find the characters in a string which are not duplicated
(29 answers)
Closed 1 year ago.
I found many question about counting characters used in a string and solutions
but how can I get all character used in string? Im new to JavaScript, im do not know to output this
for example
"English-Language" to "E,n,g,l,i,s,h,-,L,a,u,e" or "Javascript String" to "J,a,v,s,c,r,i,p,t, ,S,n,g"
Thank you..
Very simple with Set: (just cast it back to an array)
console.log([...new Set("English-Language")]);
console.log([...new Set("Javascript String")]);
And if you want it as a single string, do this:
console.log([...new Set("English-Language")].join(","));
console.log([...new Set("Javascript String")].join(","));
You can use the split function and the Set class to achieve this like so.
new Set("Javascript String".split(''))
The split('') part splits the string using an empty string as the seperator, so it just returns an array of all the characters. If you make a set from that array, it'll remove the duplicates. You can even pass the string directly to the Set constructor. If you want it as an array, just use Array.from.
you can simply use str.split("");
const str = 'English-Language';
str.split("");
Let me know if you face any future issue.
or Second option is
console.log([...new Set("English-Language")]);
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
I am trying to replace a single dash '-' character in a string with double dashes.
2015–09–01T16:00:00.000Z
to be
2015-–09-–01T16:00:00.000Z
This is the code I am using but it doesn't seem to be working:
var temp = '2015–09–01T16:00:00.000Z'
temp.replace(/-/g,'--')
In JavaScript Strings are immutable. So, when you modify a string, a new string object will be created with the modification.
In your case, the replace has replaced the characters but returns a new string. You need to store that in a variable to use it.
For example,
var temp = '2015–09–01T16:00:00.000Z';
temp = temp.replace(/–/g,'--');
Note The string which you have shown in the question, when copied, I realised that it is a different character but looks similar to – and it is not the same as hyphen (-). The character codes for those characters are as follows
console.log('–'.charCodeAt(0));
// 8211: en dash
console.log('-'.charCodeAt(0));
// 45: hyphen
The hyphen character – you have in the string is different from the one you have in the RegExp -. Even though they look alike, they are different characters.
The correct RegExp in this case is temp.replace(/–/g,'--')
Probably the easiest thing would be to just use split and join.
var temp = '2015–09–01T16:00:00.000Z'.split("-").join("--");
This question already has answers here:
Why are slice and range upper-bound exclusive?
(6 answers)
Closed 9 years ago.
I'm on Codecademy and I am learning JavaScript. I'm on substrings and I get how to do them, but I wander why substrings extracts characters from indexA up to but not including indexB. Why does it include indexA, but only up to indexB?
Please use the best layman's term for this, considering I don't have that much knowledge in this language (I'm only familiar with HTML & CSS).
var longString = "this is a long string";
var substr1 = longString.substring(0, 4); //"this"
var substr2 = longString.substring(4, 8); //" is "
This makes sense because the second substring started from where the first left off, without copying the same letter twice in both substrings. It makes it more useful in loops, for example.
Also, as everyone keeps pointing out, because "it's defined that way..."
That's because it's designed that way. Here's some documentation from MDN.
"Hello World".substring(0,5) //Hello
Simply saying
Get the substring starting with (and including) the character at the first index, and ending (but not including) the character at the second index.
Well, the method is defined in that way. Extracts from indexA up to indexB but not including.
In Javascript there are two different functions to extract a substring. One with length and other with index start and stop.
String.substring( from [, to ] )
String.substr( start [, length ] )
In all cases, the second argument is optional. If it is not provided, the substring will consist of the start index all the way through the end of the string.
Please go through this article to clear up.
http://www.bennadel.com/blog/2159-Using-Slice-Substring-And-Substr-In-Javascript.htm
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Javascript code to parse CSV data
Javascript: Splitting a string by comma but ignoring commas in quotes
I have a comma separated string like this:
car,jeep,,"ute,van",suv,truck
I need to split it and add it to array as following entries:
car
jeep
ute,van
suv
truck
I am currently first splitting the string by " and then replace , with some other character in the parts that have , only in the middle but not at the either ends.
Also I check if the length of split array is greater than 1 because in case I get strings that don't have " at all, I want to skip the replacing , part.
Then finally I join the array using "" to get the end result.
Can someone suggest any better way and efficient way to do this possibly using regex?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
add or update query string parameter
I am trying to replace the page number in the query string no matter what digit is to 1.
query string
index.php?list&page=2&sort=epub
javascript
window.location.href.replace(new RegExp("/page=.*?&/"), "page=1&")
Your code looks almost right; however:
you need to use either new RegExp or the special // regex syntax, but not both.
the replace method doesn't modify the string in-place, it merely returns a modified copy.
rather than .*?, I think it makes more sense to write \d+; more-precise regexes are generally less likely to go awry in cases you haven't thought of.
So, putting it together:
window.location.href = window.location.href.replace(/page=\d+/, "page=1");