Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 女bbbbxxxx另类亚洲,中文字幕免费观看视频,国产成人精品一区二三区

          整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          Html中的空格

          (半角的不斷行的空白格)


            (半角的空格)


            (全角的空格)

          解如何在 JavaScript 中輕松地在字符串的字符之間添加空格。

          在本文中,我們將學習如何在 JavaScript 中輕松地在字符串的字符之間包含空格。


          1. String split() 和 Split join() 方法

          要在字符串的字符之間添加空格,請在字符串上調用 split() 方法以獲取字符數組,然后在數組上調用 join() 方法以使用空格分隔符連接字符。

          例如:

          function addSpace(str) {
            return str.split('').join(' ');
          }const str1 = 'coffee';
          const str2 = 'banana';console.log(addSpace(str1)); // c o f f e e
          console.log(addSpace(str2)); // b a n a n a

          String split() 方法使用指定的分隔符將字符串拆分為子字符串數組。

          const str1 = 'coffee,milk,tea';
          const str2 = 'sun-moon-star';console.log(str1.split(',')); // [ 'coffee', 'milk', 'tea' ]
          console.log(str2.split('-')); // [ 'sun', 'moon', 'star' ]

          通過使用空字符串 ('') 作為分隔符,我們將所有字符串字符拆分為單獨的數組元素。

          const str1 = 'coffee';
          const str2 = 'banana';// Passing an empty string ('') to the split method// [ 'c', 'o', 'f', 'f', 'e', 'e' ]
          console.log(str1.split(''));// [ 'b', 'a', 'n', 'a', 'n', 'a' ]
          console.log(str2.split(''));

          String join() 方法將數組中的每個字符串與分隔符組合在一起。 它返回一個包含連接數組元素的新字符串。

          const arr = ['a', 'b', 'c', 'd'];console.log(arr.join(' ')); // a b c d
          console.log(arr.join('-')); // a-b-c-d
          console.log(arr.join('/')); // a/b/c/d

          因此,將空格字符傳遞給 join() 會在結果串聯中用空格分隔字符。

          在某些情況下,字符串已經在某些字符之間包含空格。 在這種情況下,我們的方法會在字符之間添加更多空格。

          function addSpace(str) {
            return str.split('').join(' ');
          }// These strings have spaces between some characters
          const str1 = 'co  ffee';
          const str2 = 'bana  na';console.log(addSpace(str1)); // c o     f f e e
          console.log(addSpace(str2)); // b a n a     n a

          這是因為空格 (' ') 也是一個字符,就像一個字母,調用 split() 會使其成為數組中的一個單獨元素,該元素將與其他空格組合。

          // These strings have spaces between some characters
          const str1 = 'co  ffee';
          const str2 = 'bana  na';// The space characters are separate elements of the
          // array from split()
          /**
           * [
            'c', 'o', ' ',
            ' ', 'f', 'f',
            'e', 'e'
          ]
           */
          console.log(str1.split(''));/**
           * [
            'b', 'a', 'n',
            'a', ' ', ' ',
            'n', 'a'
          ]
           */
          console.log(str2.split(''));

          如果我們想避免字符的多個間距,我們可以在 split() 和 join() 之間插入對 filter() 方法的調用。

          function addSpace(str) {
            return str
              .split('')
              .filter((item) => item.trim())
              .join(' ');
          }// The strings have spaces between some characters
          const str1 = 'co  ffee';
          const str2 = 'bana  na';console.log(addSpace(str1)); // c o f f e e
          console.log(addSpace(str2)); // b a n a n a

          Array filter() 方法返回一個新數組,該數組僅包含原始數組中的元素,傳遞給 filter() 的測試回調函數為其返回真值。 對空格 (' ') 調用 trim() 會產生一個空字符串 (''),這在 JavaScript 中不是真值。 因此,從 filter() 返回的結果數組中排除了空格。

          小費

          在 JavaScript 中,只有六個假值:false、null、undefined、0、''(空字符串)和 NaN。 其他所有值都是真實的。


          2. for...of 循環

          對于更命令式的方法,我們可以使用 JavaScript for...of 循環在字符串的字符之間添加一個空格。

          function addSpace(str) {
            // Create a variable to store the eventual result
            let result = '';  for (const char of str) {
              // On each iteration, add the character and a space
              // to the variable
              result += char + ' ';
            }  // Remove the space from the last character
            return result.trimEnd();
          }const str1 = 'coffee';
          const str2 = 'banana';console.log(addSpace(str1)); // c o f f e e
          console.log(addSpace(str2)); // b a n a n a

          為了處理前面討論的場景,其中字符串在某些字符之間有空格,請在每次迭代的字符上調用 trim(),并添加一個 if 檢查以確保它是真實的,然后將它和空格添加到累積結果中:

          function addSpace(str) {
            // Create a variable to store the eventual result
            let result = '';  for (const char of str) {
              // On each iteration, add the character and a space
              // to the variable    // If the character is a space, trim it to an empty
              // string, then only add it if it is truthy
              if (char.trim()) {
                result += char + ' ';
              }
            }  // Remove the space from the last character
            return result.trimEnd();
          }const str1 = 'co  ffee';
          const str2 = 'bana  na';console.log(addSpace(str1)); // c o f f e e
          console.log(addSpace(str2)); // b a n a n a


          關注七爪網,獲取更多APP/小程序/網站源碼資源!

          除字符串左右兩端的空格, 在PHP和Golang里面可以輕松地使用 trim、ltrim 或 rtrim, 但在JavaScript中卻沒有這三個內置方法, 需要手工編寫。

          下面的實現方法是用到了正則表達式, 效率不錯, 并把這三個方法加入String對象的內置方法中去。

          寫成類的方法格式如下:(str.trim();)

          <script type="text/javascript">
                 //刪除左右兩端的空格
             String.prototype.trim=function(){
                 return this.replace(/(^\s*)|(\s*$)/g, "");
             }
                  //刪除左邊的空格
             String.prototype.ltrim=function(){
                 return this.replace(/(^\s*)/g,"");
             }
                 //刪除右邊的空格
             String.prototype.rtrim=function(){
                 return this.replace(/(\s*$)/g,"");
             }
          </script>


          寫成函數可以這樣:(trim(str))


          主站蜘蛛池模板: 国产福利91精品一区二区| 无码精品人妻一区二区三区免费看| 中文字幕Av一区乱码| 亚洲精品精华液一区二区| 成人免费区一区二区三区| 精品一区二区三区视频在线观看| 精品欧洲av无码一区二区三区| 亚洲人成网站18禁止一区 | 日韩精品一区二区三区中文版 | 人妻久久久一区二区三区| 日韩动漫av在线播放一区| 在线视频亚洲一区| 国产乱码精品一区二区三区四川人| 一区二区在线视频| 亚洲一区动漫卡通在线播放| 国产AV午夜精品一区二区入口| 久久精品国产一区二区三区不卡 | 精品国产日产一区二区三区 | 亚洲制服中文字幕第一区| 秋霞午夜一区二区| 日韩电影一区二区三区| 亚洲AV无码第一区二区三区| 久久99精品波多结衣一区| 久久久精品人妻一区二区三区蜜桃| 亚洲国产精品一区二区久久| 2020天堂中文字幕一区在线观| 无码少妇一区二区性色AV| 国产精品香蕉在线一区| 精品亚洲av无码一区二区柚蜜| 欧美成人aaa片一区国产精品| 性色av闺蜜一区二区三区| 国产第一区二区三区在线观看| 风流老熟女一区二区三区| 一区二区在线免费视频| 91麻豆精品国产自产在线观看一区| 成人无码AV一区二区| 亚洲综合一区二区国产精品| 蜜臀AV在线播放一区二区三区| 亚洲国产欧美国产综合一区 | 精品无码国产一区二区三区麻豆| 国产福利一区二区|