2016-02-27 70 views
2

如何獲得第一個8個字符和最後8個字符從srting使用JavaScript?如何獲得前8個字符和最後8個字符串使用JavaScript?

我將這段代碼用於php,我如何將這段代碼應用於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"); 
+0

究竟你嘗試過在JS寫了這麼遠嗎? –

回答

0

您可以使用String.sliceString.substring

你的情況,這應該工作:

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 
2

您就可以使用子做這個:

var str = "some string with many chars"; 
var newstr=str.substring(0,8)+str.substr(str.length-8); 
0

使用相同的變量名,你將有:

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); 
相關問題