【问题标题】:How to get first letter from string using jquery [duplicate]如何使用jquery从字符串中获取第一个字母[重复]
【发布时间】:2013-09-24 03:04:52
【问题描述】:

我对 jquery 的了解很差。我在下面提到了脚本

var header = $('.time'+col).text();
alert(header);

我从如何获取第一个字母(例如 1)中得到字符串为“109:00AM”。 你能帮帮我吗?

【问题讨论】:

    标签: jquery


    【解决方案1】:

    试试charAt(0)like

    var header = $('.time'+col).text();
    alert(header.charAt(0));
    

    你也可以用substring点赞

    alert(header.substring(1));
    

    正如@Jai所说,你也可以使用slice

    alert(header.slice(0,1));
    

    【讨论】:

    • .slice(0, 1) 太...
    • 谢谢@Jai 我已经在我的回答中添加了这个
    • 投反对票的不是我。看到我喜欢你的评论。
    • FYI alert(header.substring(1)) 在你的帖子中给出了整个字符串 except 第一个字符,我想你想要 alert(header.substring(0,1 )); - 子字符串的语法是 (start,end) 所以你只想要它从字符 0 开始的第一个字符到字符 1 结束(不包括字符 1)
    猜你喜欢
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2019-01-16
    • 2019-09-10
    相关资源
    最近更新 更多