8个尽可能简单JavaScript字符串方法

JavaScript is not what it used to be when it first came out. It is so much better. So it makes sense that there is so much to learn.

JavaScript并不是它最初问世时的样子。 好多了。 因此,有很多东西要学习是有道理的。

As a front end developer, learning JavaScript is terrifying but crucial. And if you are anything like me, you don’t know where to go, where to start. Thousands of resources to choose from which just makes us anxious and not learn anything. We get stuck in tutorial hell.

作为前端开发人员,学习JavaScript既可怕又至关重要。 而且,如果您像我一样,则不知道去哪里,从哪里开始。 成千上万的资源可供选择,这只会使我们感到焦虑,什么也学不到。 我们陷入了教程的地狱。

Here is something that helped me get better at the arrays. Hope it helps you too. If you want to know more about JavaScript functions or arrays here are the quick and simple articles.

这是一些使我在阵列方面变得更好的东西。 希望它对您也有帮助。 如果您想了解有关JavaScript函数数组的更多信息,请阅读快速简单的文章。

So without further ado, let’s begin.

因此,事不宜迟,让我们开始吧。

1.长度 (1. Length)

The length property returns the number of characters in the string including whitespace.

length属性返回字符串中包含空格的字符数。

const str = "Hello World";
str.length//Returns 11

2.切片 (2. Slice)

The slice() method extracts a part of the string and returns the extracted part in a new string. It takes two parameters. It is similar to making cuts in a loaf of bread and extracting the slices within. But remember that the indexing starts with zero.

slice()方法提取字符串的一部分,并将提取的部分返回到新字符串中。 它有两个参数。 这类似于切一条面包,然后在其中切成薄片。 但是请记住,索引从零开始。

const str = "Hello World";
str.slice(2 , 5);//Returns llo

In the above example, we can imagine cutting at positions 2 and 5 and the part between it (llo) is returned.

在上面的示例中,我们可以想象在位置2和5处进行切割,然后返回其之间的部分(llo)。

3.更换 (3. Replace)

The replace() method replaces a specified value with another value in a string.

replace()方法将指定的值替换为字符串中的另一个值。

const str = 'YouAreAwesome';
str.replace('Awesome', 'Beautiful');//returns 'YouAreBeautiful" because you are :)

4.改变大小写 (4. Changing Case)

toUpperCase() and toLowerCase() are the methods used to convert a string to uppercase or lowercase. This can be helpful in capitalizing first letter of names.

toUpperCase()和toLowerCase()是用于将字符串转换为大写或小写的方法。 这有助于大写姓名的首字母。

const name = 'Shelby';
name.toUpperCase();
//SHELBYname.toLowerCase();
//shelby

5. Concat (5. Concat)

The concat() method is used to join two or more strings. This comes handy when you have a name of a person and want to greet them.

concat()方法用于连接两个或多个字符串。 当您有一个人的名字并想向他们打招呼时,这将很方便。

const str = 'Tim';
str.concat('IsAwesome');//TimIsAwesome

6.修剪 (6. Trim)

The trim() method is used to remove white-space from either side of a string.

trim()方法用于删除字符串两侧的空白。

const str = '    JavaScriptIsHard    ';
str.trim();//JavaScriptIsHard

7.查拉特 (7. Charat)

The charAt() method returns the character at a specified index in a string.

charAt()方法返回字符串中指定索引处的字符。

const best = 'JavaScript';
best.charAt(6);// returns c

8.拆分 (8. Split)

The split() method converts string into an array. You have to pass a character such as comma (,) or space to tell where to split the string. If nothing is passed, the string is split between each character.

split()方法将字符串转换为数组。 您必须传递一个字符,例如逗号(,)或空格,以告知在何处分割字符串。 如果未传递任何内容,则在每个字符之间分割字符串。

const msg = 'Eight,Methods,Are,Done';
msg.split(',');// returns the array ["Eight", "Methods", "Are", "Done"]

结论 (The Conclusion)

So these were the eight JavaScript string methods as simple as possible. This helped me in my learning journey. And I believe it will be of importance to you, too.

因此,这是八种尽可能简单JavaScript字符串方法。 这对我的学习旅程有所帮助。 我相信这对您也很重要。

JavaScript can be daunting, especially if you are a beginner. But some simple guides can help us reduce the anxiety we get during the learning process.

JavaScript可能令人生畏,特别是如果您是初学者。 但是一些简单的指南可以帮助我们减轻学习过程中的焦虑感。

Further, if you want to know more about JavaScript functions or arrays you can read these short articles.

此外,如果您想进一步了解JavaScript函数数组 ,可以阅读这些简短文章。

翻译自: https://medium.com/front-end-weekly/8-javascript-string-methods-as-simple-as-possible-4239524035e8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值