Javascript 如何获取字符串的第一个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3427132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:34:38  来源:igfitidea点击:

How to get first character of string?

javascript

提问by Simon

I have a string, and I need to get its first character.

我有一个字符串,我需要获取它的第一个字符。

var x = 'somestring';
alert(x[0]); //in ie7 returns undefined

How can I fix my code?

我该如何修复我的代码?

回答by Daniel Vandersluis

What you want is charAt.

你想要的是charAt.

var x = 'some string';
alert(x.charAt(0)); // alerts 's'

回答by Dustin Laine

In JavaScript you can do this:

在 JavaScript 中,您可以这样做:

const x = 'some string';
console.log(x.substring(0, 1));

回答by Shailesh Sonare

You can use any of these.

您可以使用其中任何一种。

There is a little difference between all of these So be careful while using it in conditional statement.

所有这些之间都有一点不同所以在条件语句中使用它时要小心。

var string = "hello world";
console.log(string.slice(0,1));     //o/p:- h
console.log(string.charAt(0));      //o/p:- h
console.log(string.substring(0,1)); //o/p:- h
console.log(string.substr(0,1));    //o/p:- h
console.log(string[0]);             //o/p:- h


var string = "";
console.log(string.slice(0,1));     //o/p:- (an empty string)
console.log(string.charAt(0));      //o/p:- (an empty string)
console.log(string.substring(0,1)); //o/p:- (an empty string)
console.log(string.substr(0,1));    //o/p:- (an empty string)
console.log(string[0]);             //o/p:- undefined

回答by ?ukaszW.pl

const x = 'some string';
console.log(x.substring(0, 1));

回答by Alexis

Example of all method

所有方法的示例

First: string.charAt(index)

第一string.charAt(index)

Return the caract at the index index

返回索引处的caract index

var str = "Stack overflow";

console.log(str.charAt(0));

Second: string.substring(start,length);

第二string.substring(start,length);

Return the substring in the string who start at the index startand stop after the length length

返回字符串中从索引开始start到长度结束的子串length

Here you only want the first caract so : start = 0and length = 1

在这里你只想要第一个caract:start = 0length = 1

var str = "Stack overflow";

console.log(str.substring(0,1));

Alternative: string[index]

替代方案string[index]

A string is an array of caract. So you can get the first caract like the first cell of an array.

字符串是一个字符数组。所以你可以像数组的第一个单元格一样获得第一个字符。

Return the caract at the index indexof the string

返回index字符串索引处的字符

var str = "Stack overflow";

console.log(str[0]);

回答by Eton B.

var x = "somestring"
alert(x.charAt(0));

The charAt() method allows you to specify the position of the character you want.

charAt() 方法允许您指定所需字符的位置。

What you were trying to do is get the character at the position of an array "x", which is not defined as X is not an array.

您试图做的是在数组“x”的位置获取字符,该字符未定义为 X 不是数组。

回答by yckart

You can even use sliceto cut-off all other characters:

你甚至可以slice用来截断所有其他字符:

x.slice(0, 1);

回答by Swathi

var str="stack overflow";

firstChar  = str.charAt(0);

secondChar = str.charAt(1);

Tested in IE6+, FF, Chrome, safari.

IE6+FFChromesafari 中测试

回答by AmazingDayToday

Try this as well:

也试试这个:

x.substr(0, 1);

回答by edelans

x.substring(0,1)

x.substring(0,1)

Details

细节

substring(start, end)extracts the characters from a string, between the 2 indices "start" and "end", notincluding "end" itself.

substring(start, end)从字符串中提取两个索引“start”和“end”之间的字符,包括“end”本身。

Special notes

特别说明

  • If "start" is greater than "end", this method will swap the two arguments, meaning str.substring(1, 4) == str.substring(4, 1).
  • If either "start" or "end" is less than 0, it is treated as if it were 0.
  • 如果“start”大于“end”,则此方法将交换两个参数,即 str.substring(1, 4) == str.substring(4, 1)。
  • 如果“开始”或“结束”小于 0,则将其视为 0。