This question already has answers here:
How can I get last characters of a string
(25 answers)
Closed 5 years ago.
This is a trivial problem if we are storing string in a variable. But out of curiosity, I need to understand how to achieve this without storing string in variable?
// To get first few characters of a string, we can do something like this:
var x = "amangupta".substring(0,7); //amangup
// How to get last 7 characters
var x = "amangupta".substring(this.length - 7, this.length); // does not work, looking for similar approach
var str = "amangupta";
var x = str.substring(str.length - 7, str.length); // will work fine
How to get last 7 characters
Try
"amangupta".slice(-7)
Without an extra variable you need to use the string again
"amangupta".substring("amangupta".length - 7, "amangupta".length);
Related
This question already has answers here:
The .replace() method does change the string in place [duplicate]
(3 answers)
Replace method doesn't work
(4 answers)
Closed 23 days ago.
The goal of this was to change the retrieved letters into upper case. I know this works if I stored lines 6 and 7 inside a variable then replace the letters inside the variable called string, but I wanted to know why this code doesn't work instead.
JavaScript strings are not mutable, meaning you can't change the value of a character at a certain index. You'll need to make a new string made of these values.
let string = 'lowercasestring'; // Creates a variable and stores a string.
let letterOne = string.indexOf('l'); // Should store as 0.
let letterTwo = string.indexOf('s'); // Should store as 7.
// mutiple lines for readability
string = string[letterOne].toUpperCase() +
string.slice(letterOne + 1, letterTwo) +
string[letterTwo].toUpperCase() +
string.slice(letterTwo + 1);
console.log(string);
Output:
"LowercaSestring"
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)
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);
This question already has answers here:
Repeat a string in JavaScript a number of times
(24 answers)
Closed 4 years ago.
i want to display a string as many times I have a generated variable. Therefore I'd like to do something like that, which doesn't work
var shower_total = 7; // this gets generated, but for simplification...
var uhrzeit = "<p class='clock_item'>Foo</p>";
document.getElementById("uhrzeit_block").innerHTML =5*uhrzeit;
That's why I tried looping it but that doesn't work neither
document.getElementById("uhrzeit_block").innerHTML =
for(b=0, b <= shower_total; b++){
uhrzeit
};
What do I do wrong or what would be a possible - beginner-compatible - solution. Thanks!
You could use String#repeat instead of a multiplication of a string. This does not work for value who can not converted to a number.
var uhrzeit = "<p class='clock_item'>Foo</p>";
console.log(uhrzeit.repeat(5));
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