获取字符串的最后3个字符 [英] Getting the last 3 characters of a string

查看:144
本文介绍了获取字符串的最后3个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问,如何在javascript中提取字符串的最后三(3)个字符?
可以说:
我有:var name;

如果重新分配名称,则名称始终可以更改;我想从右边获取最后的三(3).例如,如果名称="JAMES",我想获取"MES"

谢谢.

Please, how can I extract the last three(3) characters for a changing string in javascript?
lets say:
I have: var name;

name can alsways changed if it is reassigned; I want to get the last three(3) from the right. example if name = "JAMES", I want to get "MES"

Thanks.

推荐答案

使用string.substring.

参见 http://www.w3schools.com/jsref/jsref_substring.asp [
Use string.substring.

See http://www.w3schools.com/jsref/jsref_substring.asp[^]


尝试 ^ ].

顺便说一句,AFAIR,不支持跨浏览器.因此,请进行相应的检查/使用看起来现在所有浏览器都支持它.现在 [ ^ ].

嗯,试试看:
Try Javascript substring method[^].

BTW, AFAIR, it''s not crossbrowser supported. So, do check/use it accordingly Looks like it is supported by all browsers now[^].

Hmmm, try this:
name.substr(-3); // should return MES


查看此网页上的子字符串方法;
http://www.electrictoolbox.com/javascript-substr/ [
Check out this web page for substrings method;
http://www.electrictoolbox.com/javascript-substr/[^]

So maybe at first you can get the string length by using name.length and then use the substring method.


这篇关于获取字符串的最后3个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆