JavaScript 中的 String startsWith() 方法

2025-06-07

JavaScript 中的 String startsWith() 方法

SamanthaMing.com 的 CodeTidbit

如果你需要检查一个字符串是否以另一个字符串开头,可以使用 ES6 的startsWith方法。我非常喜欢这个方法,因为它直观地展现了它的全面性。即使你没有任何技术背景,只需阅读代码,就能更容易地推断出发生了什么,相比之下indexOf🤓

我非常喜欢 JavaScript 的发展方向。它不仅引入了所有这些实用的方法,还不断改进,使其更易于理解。这就是我们让科技更容易普及的方式。让它更容易学习。太喜欢了!😍

const lunch = '🥗 🥪 🍮'

// Old Way
lunch.indexOf('🥗') === 0 // true

// ✅ ES6 Way
lunch.startsWith('🥗') // true
Enter fullscreen mode Exit fullscreen mode

startsWith() 参数

startsWith方法接受两个参数:

  1. 搜索值
  2. 起始索引

1. 搜索价值

此字段为必填字段。您可以在此处输入搜索值。搜索值可以是单个字符,也可以是更长的字符。我们来看一些示例

单个角色

const name = 'Samantha Ming';

name.startsWith('S'); // true
name.startsWith('M'); // false
Enter fullscreen mode Exit fullscreen mode

多个角色

const name = 'Samantha Ming';

name.startsWith('Sam'); // true
name.startsWith('Min'); // false
Enter fullscreen mode Exit fullscreen mode

多个单词

const name = 'Samantha Ming';

name.startsWith('Samantha M'); // true
name.startsWith('antha Min'); // false
Enter fullscreen mode Exit fullscreen mode

整个字符串

const name = 'Samantha Ming';

name.startsWith('Samantha Ming'); // true
Enter fullscreen mode Exit fullscreen mode

超出字符串的长度

const name = 'Samantha Ming';

name.startsWith('Samantha Ming is the best'); // false ← 😅
Enter fullscreen mode Exit fullscreen mode

2. 起始索引

因此,默认情况下,您的起始索引将是0。但是使用此参数,您可以使其从其他起始位置开始。我们来看几个例子。

默认索引 (0)

const name = 'Samantha Ming';

name.startsWith('S'); // true
name.startsWith('S', 0); // true
Enter fullscreen mode Exit fullscreen mode

从第一个索引开始

对于编程新手来说,请注意 JavaScript 是从零开始的。这意味着计数从 开始0。所以第一个字符在0index 处,第二个字符也在1index 处 🤓

const name = 'Samantha Ming';

name.startsWith('am', 1); // true
name.startsWith('am'); // false
Enter fullscreen mode Exit fullscreen mode

从第二个索引开始

按照我们从零开始的计数,第二个索引等于第三个字符。

const name = 'Samantha Ming';

name.startsWith('ma', 2); // true
name.startsWith('ma'); // false
Enter fullscreen mode Exit fullscreen mode

负起始指数

所以负索引不行。我只是想测试一下负索引是否也能像上面那样工作,slice()比如传递负索引时,它会返回最后一个字符。再次证明,别以为你能胜过 JavaScript 😂

const name = 'Samantha Ming';

name.startsWith('g', -1); // false
Enter fullscreen mode Exit fullscreen mode

我想这就是它endsWith的用途吧。以后我会在小贴士里再讲这个😜

区分大小写

要记住的一件重要事情是该startWith方法区分大小写。

const name = 'Samantha Ming';

name.startsWith('sam'); // false
Enter fullscreen mode Exit fullscreen mode

浏览器支持

所有现代浏览器都支持此功能。除了……我敢肯定你猜对了——不支持 Internet Explorer 😑。你需要使用 Polyfill 或像 Babel 这样的编译器。

浏览器支持

社区意见

💬 您知道还有哪些方法可以检查字符串是否以某个内容开头?

这是我在社区里问的问题。得到了一些非常好的答案。我们来看看吧👀

使用搜索

const lunch = '🥗🥪☕️';
const search = '🥗';
lunch.slice(0, search.length) === search;
Enter fullscreen mode Exit fullscreen mode

感谢:@abraham

使用正则表达式

'some string'.match(/^some/);

// OR
(/^some/).test('some string');
Enter fullscreen mode Exit fullscreen mode

感谢:@cpt_silverfox

使用括号

如果你只想检查单个字符,可以试试这个。但请注意,当你有多个字符(例如 hel)时,此方法将不起作用。

const word = 'hello';

word[0] === 'h';
Enter fullscreen mode Exit fullscreen mode

感谢:@neutrino2211

性能检查

@gwardwell这里有一个这样的测试(在 JSPerf 上找到,我没有编写它)可以表明indexOf打击startsWith

资源


感谢阅读❤
打个招呼!Instagram | Twitter | Facebook |博客| SamanthaMing.com

文章来源:https://dev.to/samanthaming/string-startswith-method-in-javascript-1m81
PREV
[Agentica] 每个 TypeScript 开发人员都是 AI 开发人员
NEXT
将数组作为函数参数传递