Javascript字符串变量类型

While developing Javascript applications we generally need some type that can hold text and string data. We call this type of variable as a string. The string can hold names, street info, paragraph, explanation, etc. The string is the type used for text data. String variables can be defined in various ways.

在开发Javascript应用程序时,我们通常需要一些可以容纳文本和字符串数据的类型。 我们将这种类型的变量称为字符串。 该字符串可以包含名称,街道信息,段落,说明等。该字符串是用于文本数据的类型。 字符串变量可以通过多种方式定义。

name="Poftut";
name2='Poftut';
  • These are string variables

    这些是字符串变量
var val = new String("Poftut");
  • val is a String object the very similar to a string variable

    val是一个String对象,非常类似于string变量

There are methods to use with the string data type. The string data type is wrapped as a String object in Javascript to provide this method.

有一些用于字符串数据类型的方法。 字符串数据类型在Javascript中包装为String对象,以提供此方法。

获取字符串的长度 (Get The Length of String)

To get the length of the string is easy as below. Length is equal to the number of characters including spaces.

获取字符串的长度很容易,如下所示。 长度等于包含空格的字符数。

Code:

码:

var val = "Poftut";
console.log(val.length);

var val = "Poftut ";
console.log(val.length);
  • length provides the length of string

    长度提供字符串的长度

Output:

输出:

6
7
  • As we see the second line of output is 7 where the second string has space at the end

    我们看到输出的第二行是7,其中第二个字符串的末尾有空格

查找字符串的索引(Find Index of String)

If we are looking at a characters position in the string we can use indexOf function

如果我们正在查看字符串中的字符位置,则可以使用indexOf函数

Code:

码:

poem="This is a poem from me";
console.log(poem.indexOf("p"));
console.log(poem.indexOf("z"));

Output:

输出:

10
-1
  • If the char is not existed in the string -1 is returned.

    如果字符串中不存在char,则返回-1

合并字符串 (Combine Strings)

As we know strings cannot be changed after initialized so to add some string concat  can be used

众所周知,初始化后无法更改字符串,因此可以添加一些字符串concat

Code:

码:

poem="This is a poem from me.";
newpoem=poem.concat(" This is added");
console.log(newpoem);

Output:

输出:

This is a poem from me. This is added

返回带索引的字符 (Return A Char With Index)

To get a character from the specified index  charAt function is used. Index numbers start from 0.

要从指定的索引获取字符,请使用charAt函数。 索引号从0开始。

LEARN MORE  Useful Docker Command List with Examples
了解更多有用的Docker命令列表以及示例

Code:

码:

poem="This is a poem from me";
console.log(poem.charAt(1));

Output:

输出:

h

替换字符串 (Replace String)

To replace the string replace method is used. There are two parameters.

要替换字符串,请使用替换方法。 有两个参数。

Code:

码:

poem="This is a poem from me";
console.log(poem.replace("me","you"));
  • me” is the string we want to replace

    ”是我们要替换的字符串

  • “you” the new string

    “你”新的字符串

Output:

输出:

This is a poem from you

搜索字符串 (Search String)

Searching for a specified string in a string. Returns the index of the search term.

在字符串中搜索指定的字符串。 返回搜索词的索引。

Code:

码:

poem="This is a poem from me";
console.log(poem.search("me"));

Output:

输出:

20

子串 (Substring)

There are two types of substring method. Substring is used to extract some part of a string by providing index values.

子串方法有两种类型。 子字符串用于通过提供索引值来提取字符串的某些部分。

Code:

码:

poem="This is a poem from me";
console.log(poem.substring(1,2));
  • starts index for a substring

    1开始一个子串的索引

  • ends index for a substring

    子字符串的2个结束索引

Output:

输出:

h

Code:

码:

poem="This is a poem from me";
console.log(poem.substr(1,2));
  • starts index for a substring

    1开始一个子串的索引

  • is the length of the substring from start index which means we will get 2 characters

    2是从起始索引开始的子字符串的长度,这意味着我们将获得2个字符

Output:

输出:

hi

小写 (To Lower Case)

All uppercase characters can be converted into lowercase characters with toLowerCase method

可以使用toLowerCase方法将所有大写字符转换为小写字符

Code:

码:

poem="This Is a Poem From Me";
console.log(poem.toLowerCase());

Output:

输出:

this is a poem from me

大写 (To Upper Case)

All lowercase characters can be converted into uppercase characters with toUpperCase method

可以使用toUpperCase方法将所有小写字符转换为大写字符

Code:

码:

poem="This Is a Poem From Me";
console.log(poem.toUpperCase());

Output:

输出:

THIS IS A POEM FROM ME

转换为字符串 (Convert To String)

We may need numeric or other data types to convert string to manipulate them.

我们可能需要数字或其他数据类型来转换字符串以对其进行操作。

Returned object type will be a string

返回的对象类型将是字符串

Code:

码:

count=5;
console.log(count.toString());

Output:

输出:

5
 

翻译自: https://www.poftut.com/javascript-string-variable-types/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值