Javascript如何获取字符串的前三个字符 [英] Javascript How to get first three characters of a string

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

问题描述

这可能与以前的主题重复,但我找不到我真正需要的东西。

This may duplicate with previous topics but I can't find what I really need.

我想获得字符串的前三个字符。例如:

I want to get a first three characters of a string. For example:

var str = '012123';
console.info(str.substring(0,3));  //012

我想要输出这个字符串'012',但我不想用subString或类似的东西,因为我需要使用原始字符串来追加更多字符'45'。使用子字符串,它将输出01245,但我需要的是01212345。

I want the output of this string '012' but I don't want to use subString or something similar to it because I need to use the original string for appending more characters '45'. With substring it will output 01245 but what I need is 01212345.

推荐答案

var str = '012123';
var strFirstThree = str.substring(0,3);

console.log(str); //shows '012123'
console.log(strFirstThree); // shows '012'

现在您可以访问这两个。

Now you have access to both.

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

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