I mean, in theory you could try to write your own âsliceâ function instead of using the one of JS.
First question as always is: Why would you want to do that? As it means more time to develope and most likely will be not as stable as the already built-in function.
Iâve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the âpreformatted textâ tool in the editor (</>) to add backticks around text.
Can this be done without using slice or some other method? Absolutely. Unfortunately, manipulating strings isnât as simple in JS as it is one some languages (like C, etc.) but you can certainly do it.
I would imagine that in JS, it would involve creating an array of the string - either do that and use a loop to pop off what you donât need or create an empty array and push on what you do need. Then convert it to a string. Of course, those involve using other prototype methods like push and pop - which for some reason you donât like. You could also do the second method, slowly concatenating characters onto the string with a loop, but that is messy and can be resource intensive.
But JS is JS - donât try to turn it into something else. You donât buy a Ferrari and say you donât like gasoline so you want to hook a horse up to the front to move it around. If thatâs what you wanted, you shouldnât have bought a car. JS is an amazing language with (as with any other language) strengths and weaknesses - you should be playing to its strengths, donât try to dwell on its weaknesses.
There are languages where you can do low level manipulations like that - this ainât it. JS tries to free you up from those things so you can think about the bigger picture. Sure, it comes with some cost, but there are also benefits. Every language makes different trade offs.
Yeah your right but as a beginner i am trying to understand the languages in deep. My point was that slice method is doing something in the background for us right? and i am curious about that thing that slice do and make the work a hell lot easier for us thatâs all.
Iâm not sure what you mean by âin the backgroundâ. Slice is pretty explicit about what it does.
The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end ( end not included) where start and end represent the index of items in that array. The original array will not be modified.
Slice copies the requested portion of the string or array.
Its a heck of a lot easier than manually copying the portion you need.
The exact way slice works internally would depend upon the JavaScript engine. But it does not matter exactly how the memory copying semantics work internally. That is not something you want to do manually in JS. And its not really something that you have the ability to do manually in JS.
You canât, and shouldnât, write
// C function to do the same
int truncateString(const char *str, int num, char **new_str) {
// old string length
size_t str_length;
for (str_length = 0; str[str_length]; str_length++) {}
// allocate new string
size_t new_str_length = str_length;
if (str_length > num) {
new_str_length = num + 3;
}
*new_str = calloc(new_str_length + 1, sizeof(char));
// copy old string
memcpy(*new_str, str, num);
// add dots if needed
if (str_length > num) {
memcpy(&(*new_str[num]), "...", 3);
}
return 0;
(Note, this was written off the top of my head without testing. It probably has some compile bugs, but the idea here is that manual string copying is hideous and you donât want to do it. JS is designed to protect you from manual memory management.)
âHowâ deep?
To copy a string by hand, you can just use a for-loop. Same for slicing.
However if you want to got deeper⌠well JS is a high level script-language.
In a computer you got the actual bit-operations done in the CPU, which are determined by the physical construction of the chip. On top of that you got machine-code, assembler and whatever additional programs work inbetween (like the interpreter for JS and interpreters can create some inbetween-code themselfâŚ)
And not many people work on those levels because itâs really tedious and weird and frankly high-level programming languages were developed so people donât need to understand memory-allocation and -adressing by hand.
So you can go VERY deep if you want to. But itâs not recommended unless you are planning on a specific career. I like Machine Learning, so Iâll go with Python and let it do most of the work, without bothering on how I would write a function to create and optimize a million parameters.
string.slice(a, b)? Well a specific range to repeat an action is given â sounds like a for-loop.
More complex operations like regular expression? Search-Algorithms? More? WellâŚ
I wouldnât say itâs that much easier using a build-in method than it is using a for loop and string concatenation (but more convenient and declarative for sure).
I would suggest you try to solve it using something like a for loop and post your code (working or not).
I would use that as well I think, although itâs fairly potato, potahto I guess.
On a completely unrelated note: Was someone from myspace on the standards committee when they came up with String.prototype.blink()? Surely there are more pressing matters than making text blink when it comes to string manipulation?
The end result is identical, yes. Which also speaks to the OPâs question of other ways to do this. I think my question was more toward the OP: is this a question of âin what ways could i do this involving an array?â Or is there a reason youâre not considering string manipulation methods, in particular non-mutating ones?