Remove content after . from string [duplicate] - javascript

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 3 years ago.
I have a string var x = 2019-02-14T21:06:06.400Z
Another string y = 2019-02-14T21:06:06.44500Z
I need to remove content after the dot i cant slice it because the dot might come after 4 or 5 or 6 or n characters

Use split:
let [beforeDot] = "2019-02-14T21:06:06.400Z".split(".");
console.log(beforeDot);

You can use .split() in conjunction with an array access by index.
Example:
"2019-02-14T21:06:06.44500Z".split('.')[0]

A simple solution.
const strIn = "2019-02-14T21:06:06.44500Z";
const index = strIn.indexOf(".");
const strOut = strIn.substr(0, index);
console.log(strOut);

Related

How to split a string with arbitrary arguments? [duplicate]

This question already has answers here:
How can I split a string into segments of n characters?
(17 answers)
How do you use a variable in a regular expression?
(27 answers)
Closed 2 years ago.
I'm struggling with a very simple problem.
lines = "hogefoobarwai"
I want to cut this string into 4 characters.
Like this.
hoge, foob, arwa, i
How to split?
I try to use split() with regex.
let vars = lines.match(/.{4}/g);
This is good. But if something like {4} is variable, it won't work.
for example
length = 6
let vars = lines.match(/.{length}/g);
this shows literally /.{length}/.
If anyone can tell me what it is, please let me know.
You could take a minimum length of one (for getting smaller substrings) and the length and build a new regular expression.
const
lines = "hogefoobarwai",
length = 4,
parts = lines.match(new RegExp(`.{1,${length}}`, 'g'));
console.log(parts);
try using a dynamically generated Regex:
const newRegEx = new RegEx('{' + length + '}', g)

how to convert "12A,13B,14C" to [12A,13B,14C] in javascript [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 5 years ago.
I have a string like below.
sections = "12A,13B,14C"
But I need [12A, 13B, 14C] for future use. How to create array from above string?
You have to use split method for that to split your string data into array values, have a look to updated code
var sections = "12A,13B,14C";
sections = sections.split(',');
console.log(sections);
Hope this will help! Just use split method of JS.
sections = "12A,13B,14C"
let array = sections.split(',');
console.log(array)

String to array filled with chars [duplicate]

This question already has answers here:
Split string into array [duplicate]
(10 answers)
Closed 5 years ago.
So I have a string similar to the following:
var mystring = "aasfeeeffdbals";
and I need to make it an array filled with the individual chars. I know what I can do that with the following method:
var charArray = []
for (var i = 0; i<mystring.length; i++) {
charArray.push(mystring[i]);
}
QUESTION: My understanding of a string is that it already in essence is an array of characters put together, so might there be a more straight forward way of creating a new array with the characters from the string?
var chars = "aasfeeeffdbals".split(''); will bring you wanted result

Javascript split number and string and keep both [duplicate]

This question already has answers here:
Javascript and regex: split string and keep the separator
(11 answers)
Splitting a string into chunks by numeric or alpha character with JavaScript
(3 answers)
Closed 6 years ago.
I have a string
var houseNumber = '13a';
I want to split the addition from the number so I can keep it in a other field.
How can I split this value without losing the number or the addition? At the end I would like to have 2 fields with the following type:
var houseNumber = 13; //Number
var addition = 'a'; //String
There are many questions about this, but I can't find one where both values has to be saved. That's why I created a new question.
Use the following code
var string_a = "jkjkhj89898";
var x = string_a.match(/[^\d]+|\d+/g);
console.log(x)
working fiddle.
Thanks

JavaScript / jQuery: how to split string with multiple values [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
I have a string that always contains 8 variable values that are separated with a hypen (-) like in the following example:
5-2-2-2-2-2-2-1
What is the best way to split this into 8 separate values so that I can use each of them in a variable if the values can be either an integer or the value 'n/a' ?
Many thanks for any help with this, Tim.
var str = '5-2-2-2-2-2-2-1';
var parts = str.split('-');
for (var i=0;i<parts.length;i++){
console.log(parts[i]);
}
You are searching for the String.split() method

Categories

Resources