只保留字符串中的前n个字符? [英] Keep only first n characters in a string?

查看:136
本文介绍了只保留字符串中的前n个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript中是否有一种方法可以删除字符串的结尾?

Is there a way in JavaScript to remove the end of a string?

我只需保留字符串的前8个字符并删除其余字符。

I need to only keep the first 8 characters of a string and remove the rest.

推荐答案

您正在寻找JavaScript的字符串方法 substring

You are looking for JavaScript's String method substring

例如

'Hiya how are you'.substring(0,8);

返回从第一个字符开始并在第9个字符之前结束的字符串 - 即'Hiya how' 。

Which returns the string starting at the first character and finishing before the 9th character - i.e. 'Hiya how'.

子字符串文档

这篇关于只保留字符串中的前n个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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