From ebf68d23bc9c370d60dc8b3dfedb94b76724ed25 Mon Sep 17 00:00:00 2001 From: Leon Xu <6479451-leonxu260@users.noreply.gitlab.com> Date: Fri, 27 Aug 2021 13:01:21 -0700 Subject: [PATCH] doc: create sting type section --- source/_posts/JavaScript.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/source/_posts/JavaScript.md b/source/_posts/JavaScript.md index e6ec4ca..a96426f 100644 --- a/source/_posts/JavaScript.md +++ b/source/_posts/JavaScript.md @@ -113,4 +113,24 @@ let credits = 5 + 4 + (2 * 3); - 整数和实数是相同的类型(没有int和double)。JS中的所有数字都是浮点数。 - 相同的运算符: + - * / % ++ -- = += -= *= /= %= 和Java的[优先级](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)类似。 - 许多操作符会自动转换类型:"2" * 3 = 6 -- NaN("非数字")是具有未定义数值结果的操作的返回值(例如,将字符串除以数字) \ No newline at end of file +- NaN("非数字")是具有未定义数值结果的操作的返回值(例如,将字符串除以数字) + +## 字符串类型 +```javascript +let nickName = "Sparky O'Sparkz"; // "Sparky O'Sparks" +let fName = nickName.substring(0, s.indexOf(" ")); // "Sparky" +let len = nickName.length; // 15 +let name = 'Pikachu'; // can use "" or '' +``` +JavaScript可以用的一些方法:[charAt](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/charAt), [charCodeAt](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt), [fromCharCode](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode), [indexOf](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf), [lastIndexOf](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf), [replace](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace), [split](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/split), [substring](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/substring), [toLowerCase](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase), [toUpperCase](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) + +要访问字符串s的字符,使用s[index]或s. charat (index): +```javascript +let firstLetter = puppyCount[0]; // "1" +let fourthLetter = puppyCount.charAt(3); // "p" +let lastLetter = puppyCount.charAt(puppyCount.length - 1); // "!" +``` + +虽然JS中的字符串与你在Java中使用的字符串非常相似,但也有一些特殊情况需要注意。 +- 记住长度是一个属性(而不是Java中的方法) +- 与+连接:1 + 1是2,但"1"+ 1和1 +"1"都是“11” \ No newline at end of file