如何使用jquery从字符串中获取前4个字母 [英] How get first 4 letters from string by using jquery

查看:1134
本文介绍了如何使用jquery从字符串中获取前4个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从字符串中获得前4位数字

但是它显示为null



我怎么会在这里弄错



我的尝试:



I am trying to get first 4 digit from string
but its show null

how i mistaken here

What I have tried:

alert("Result " + n_result);
      //n_result val 1500.02154
      lbtot1 = (n_result / 11)
      //lbtot1=136.36559454
      alert(lbtot1);
      var str1 = (n_result / 11);
      // str1 val = 136.36559454
      alert(str1 + " str1");


      var resu1 = str1.substring(0, 4);
      alert(resu1);
     // here he did not alert any value
     //did't show value
    // here i want to check is 4th digit/letters/chr  (136.) chr is "." or number

推荐答案

Quote:

这里我要检查的是第4位/字母/ chr(136.)chr是。或数字

here i want to check is 4th digit/letters/chr (136.) chr is "." or number



你不能在 str1 上调用 substring ,因为它是不是一个字符串。但你不需要 - 第四个字符的唯一方式是如果数字大于或等于 100 ,小于 1000 。如果 n_result 可能为负数,则数字大于 -100 且小于或等于 -10 还将有一个作为第四个字符。


You can't call substring on str1 because it's not a string. But you don't need to - the only way the 4th character will be "." is if the number is greater than or equal to 100, and less than 1000. If n_result could be negative, then numbers greater than -100 and less than or equal to -10 will also have a "." as the 4th character.

var isFouthCharDot = (lbtot1 >= 100 && lbtot1 < 1000) || (lbtot1 > -100 && lbltot1 <= -10);



作为奖励,这也可以避免使用<$ c $的文化问题c>,作为小数点分隔符。


jquery在这里无关紧要。如果要将数字转换为字符串,请调用str1.toString()
jquery is irrelevant here. If you want to convert a number into a string, call str1.toString()


这篇关于如何使用jquery从字符串中获取前4个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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