How to get first 8 char and last 8 char from srting using javascript ?
I use this code for php , How can i apply this code to javascript ?
$file_name_length = mb_strlen($file_name, 'UTF-8');
$first_char_position_last_8_char_file_name= $file_name_length-8;
$first_8_char_file_name_display = mb_substr($file_name, 0, 8,"utf-8");
$last_8_char_file_name_display = mb_substr($file_name, $first_char_position_last_8_char_file_name, 8,"utf-8");
How to get first 8 char and last 8 char from srting using javascript ?
I use this code for php , How can i apply this code to javascript ?
$file_name_length = mb_strlen($file_name, 'UTF-8');
$first_char_position_last_8_char_file_name= $file_name_length-8;
$first_8_char_file_name_display = mb_substr($file_name, 0, 8,"utf-8");
$last_8_char_file_name_display = mb_substr($file_name, $first_char_position_last_8_char_file_name, 8,"utf-8");
Share
Improve this question
edited Feb 27, 2016 at 16:34
Jagdish Idhate
7,7429 gold badges36 silver badges57 bronze badges
asked Feb 27, 2016 at 16:18
mongmong seeseemongmong seesee
1,0251 gold badge14 silver badges24 bronze badges
1
- What exactly have you tried to write in JS so far? – Evan Bechtol Commented Feb 27, 2016 at 16:22
4 Answers
Reset to default 6You can use String.prototype.substr():
var data = '12345678whatever87654321';
data.substr(0, 8); // 12345678
data.substr(-8); // 87654321
You can do it with substring like this:
var str = "some string with many chars";
var newstr=str.substring(0,8)+str.substr(str.length-8);
You can use String.slice or String.substring
In your case this should work:
var string = 'Hello nice nice world';
// with substring
console.log(string.substring(0, 8), string.substring(string.length - 8, string.length)); // Hello ni ce world
// with slice
console.log(string.slice(0, 8), string.slice(string.length - 8, string.length)); // Hello ni ce world
Using the same variable names you will have:
var file_name_length = file_name.length;
var first_char_position_last_8_char_file_name= file_name_length - 8;
var first_8_char_file_name_display = file_name.substr(0, 8);
var last_8_char_file_name_display = file_name.substr(first_char_position_last_8_char_file_name, 8);