在字符串中用jquery查找最后一个单词的第一个字母(字符串可以有多个单词) [英] Find the first letter of the last word with jquery inside a string (string can have multiple words)
问题描述
Hy,有没有办法找到字符串中最后一个单词的第一个字母?字符串是XML解析器函数的结果。在each()循环中,我得到所有节点,并将每个名称放在一个变量中,如下所示:var person = xml.find(name)。find()。text()
Hy, is there a way to find the first letter of the last word in a string? The strings are results in a XML parser function. Inside the each() loop i get all the nodes and put every name inside a variable like this: var person = xml.find("name").find().text()
现在,人持有一个字符串,可能是:
Now person holds a string, it could be:
- Anamaria Forrest Gump
- John Lock
如您所见,第一个字符串包含3个字,而第二个字符串包含2个字。
As you see, the first string holds 3 words, while the second holds 2 words.
我需要的是最后几个字母的第一个字母:G,L,
What i need are the first letters from the last words: "G", "L",
我如何做到这一点? TY
How do i accomplish this? TY
推荐答案
这应该这样做:
var person = xml.find("name").find().text();
var names = person.split(' ');
var firstLetterOfSurname = names[names.length - 1].charAt(0);
这篇关于在字符串中用jquery查找最后一个单词的第一个字母(字符串可以有多个单词)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!