2016-05-10 20 views

回答

2
var name = 'John Smith'; 
var names = name.split(' '); // split around the space 
var result = names[0].substr(0, 1) + ' ' + names[1]; 

.substr(0, 1)是這樣工作的:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/substr

將名稱連接在一起。

爲了完整,包括micha149答案:

var name = 'John Smith'; 
var names = name.split(' '); // split around the space 
var result = names[0].charAt(0) + ' ' + names[1]; 

.charAt(0)作品像這樣:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt

2

嗯。只是簡單的分裂?!

const nameParts = name.split(" "); 
const firstLetter = nameParts[0].charAt(0); 
const secondWord = nameParts[1]; 
相關問題