slice() Function in Javascript

slice() function இங்கு நாம் கொடுக்கும் index position களை பொருத்து குறிப்பிட்ட string ஐ slice செய்து எடுப்பதற்கு பயன்படுகிறது.

string.slice(start,end)

Note: slice() function இங்கு start index position மற்றும் end index position ஆகியன argument ஆக அனுபப்படுகிறது. இங்கு இரண்டு index position களை பொருத்து குறிப்பிட்ட string slice செய்து எடுக்கப்படுகிறது. இந்த function original string இல் எந்த மாற்றமும் செய்யாது.

Example1

<script>
var input = "You carry the passport to your own happiness";
var res = input.slice(0,22);
document.writeln(res);
</script>

மேலே உள்ள Example1-ஐ கவனிக்கவும் இங்கு input என்ற variable இல் "You carry the passport to your own happiness" என்ற string value ஆனது store செய்யபட்டுள்ளது.இங்கு input.slice(0,22) என்ற function பயன்படுத்தபடுகிறது, இங்கு start index position 0 மற்றும் end index position 22 என கொடுகப்பட்டுள்ளது. எனவே நமக்கு output You carry the passport என கிடைக்கிறது.

Output:

You carry the passport

Example2

<script>
var data = "I choose to make the rest of my life the best of my life";
var res = data.slice(0,36);
document.writeln(res);
</script>

மேலே உள்ள Example2-ஐ கவனிக்கவும் இங்கு data என்ற variable இல் "I choose to make the rest of my life the best of my life" என்ற string value ஆனது store செய்யபட்டுள்ளது. இங்கு data.slice(0,36) என்ற function பயன்படுத்தபடுகிறது, இங்கு start index position 0 மற்றும் end index position 36 என கொடுகப்பட்டுள்ளது. எனவே நமக்கு output I choose to make the rest of my life என கிடைக்கிறது.output ஐ கவனிக்கவும்.

Output:

I choose to make the rest of my life

Comments