Convert the string to two string by specific link - javascript

I have string like below
123443GH
I want to slice it from end to seperate into two strings
For example if my input is 2 , It must return two arrays 123443 and GH , if my input is 3 it must return 12344 and 3GH
Rightnow I use two different codes , Is there any other fastest way to do it ?
str.slice(0,-2)
and
str.slice(-2)

I ran this through a quick benchmark.
It looks like providing the end-index is faster than not.
See here: https://www.measurethat.net/Benchmarks/Show/7513/0/slice-withwithout-end-index

Related

Dynamically splitting an array depending on character count

I have an array like so:
const testArray = [ 'blah', 'abc​testtt​​', 'atestc​', 'testttttt' ]
I would like to split the string once it reaches a certain character count, for example lets use 10 characters. Also, I would like the output to swap itself to be able to use within 10 characters. Please see the expected output below if this doesn't really make sense to you. Please assume that each item in the array will not be above 10 characters just for example purpose.
So once the testArray reaches 10 characters I would like the next item to be under a new variable maybe? Not sure if thats the best way of doing this.
Something like this maybe? Again this may be very inefficient, if so please feel free to use another method.
const testArray = [ 'blah', 'abctesttt​​', 'atestc', 'testttttt' ]
if ((testArray.join('\n')).length) >= 10 {
/* split the string into parts and store it under a variable maybe?
console.log((the_splitted_testArray).join('\n')); */
}
Expected output:
"blah
atestc" //instead of using "abctesttt" it would use "atestc" as it's the next element in the array and it also avoids reaching the 10 character limit, if adding "atestc" caused the character limit to go over 10, I would like it to check the next element and so on
"abctesttt" // it can't add the remaining "testttttt" since that would cause the character limit to be reached
"testttttt"
First of all, as you can't create a new variable out of nowhere at run time, you are probably going to use a "parent"-array, which then contains the actual strings with a length of 10 maximally.
For the grouping you probably have to design an algorithm yourself. My first idea for an algorithm is something like below. Probably not the best and most efficient way (as the description of "efficient" depends on your personal priorities), but feel free to optimise it yourself :)
Walk through $testArray[], sort all strings into a new two-dimensional array: $stringLength[$messagesWithSameLength[]]. Like array(1=>array('.','a'),2=>array('hi','##',...),...)
Now, always try to get as many strings together as possible. Start with one of the longest strings, calculate the remaining space and get a string suiting best into it. If none fits, start a new group.
Always try to use the space as good as possible

How to force an integer into two digits exactly in JavaScript?

The problem is from my trying to convert number into a time format. I'd like to put time like "8:5" into "08:05". Any elegant JavaScript code?
Use split, map, slice and join
"8:5".split(":").map((s)=>("0"+s).slice(-2)).join(":"); //prints 08:05
You can invoke this at the blur/change event of your input box.

How to "unformat" a numerical string? JavaScript

So I know how to format a string or integer like 2000 to 2K, but how do I reverse it?
I want to do something like:
var string = "$2K".replace("/* K with 000 and remove $ symbol in front of 2 */");
How do I start? I am not very good regular expressions, but I have been taking some more time out to learn them. If you can help, I certainly appreciate it. Is it possible to do the same thing for M for millions (adding 000000 at the end) or B for billions (adding 000000000 at the end)?
var string = "$2K".replace(/\$(\d+)K/, "$1000");
will give output as
2000
I'm going to take a different approach to this, as the best way to do this is to change your app to not lose the original numeric information. I recognize that this isn't always possible (for example, if you're scraping formatted values...), but it could be useful way to think about it for other users with similar question.
Instead of just storing the numeric values or the display values (and then trying to convert back to the numeric values later on), try to update your app to store both in the same object:
var value = {numeric: 2000, display: '2K'}
console.log(value.numeric); // 2000
console.log(value.display); // 2K
The example here is a bit simplified, but if you pass around your values like this, you don't need to convert back in the first place. It also allows you to have your formatted values change based on locale, currency, or rounding, and you don't lose the precision of your original values.

How to convert the Date into words?

Can anyone help me regarding the conversion of Date into words - e.g: '12/04/2011' convert to 'one twelve zero four two zero one one' ...?
Can we do this using javascript?
Do we have any plugin in jquery?
I would use an array:
var numbers = ['zero','one','two'...]
Then loop through the string containing the date (dateVar) using the array as a reference for the names.
for (i=1;i<dateVar.length;i++){
document.write( numbers[parseInt(dateVar.charAt(i))]+' ');
}
Happy to help work out the specifics if you need.
Go through the following link
http://xl.barasch.com/cCo11432.htm
and you will able to find the answer

JavaScript Concat

Via jquery / Javascript
Given a number 1234567891234
How can I concat, or truncast By removing the 2 from the left to make that number a valid integer I can insert into MySQL:'
4567891234
Thanks
This question edited by someone other than the OP, who's trying to guess at what the OP wants. Bear this in mind, but I think he wants to know:
How can I remove the first two characters from a string, using jQuery?
Assuming they want a substring of the original number:
var result = String(1234567891234).substr(2);
console.log(result); // Should be 34567891234
A crossbrowser way:
(1234567891234).toString().slice(2);

Categories

Resources