Skip to content

[js] 第2天 写一个方法去掉字符串中的空格 #6

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

写一个方法去掉字符串中的空格,要求传入不同的类型分别能去掉前、后、前后、中间的空格

Activity

haizhilin2013

haizhilin2013 commented on Apr 17, 2019

@haizhilin2013
CollaboratorAuthor

欢迎大家展开多种不同的方法

yxkhaha

yxkhaha commented on Apr 18, 2019

@yxkhaha
        var str = '  abc d e f  g ';
        function trim(str) {
            var reg = /\s+/g;
            if (typeof str === 'string') {
                var trimStr = str.replace(reg,'');
            }
            console.log(trimStr)
        }
        trim(str)
qq674785876

qq674785876 commented on May 5, 2019

@qq674785876

var trim = function(str){
return str.replace(/\s*/g,"");
}
str.replace(/\s*/g,""); //去除字符串内所有的空格
str.replace(/^\s*|\s*$/g,""); //去除字符串内两头的空格
str.replace(/^\s*/,""); //去除字符串内左侧的空格
str.replace(/(\s*$)/g,""); //去除字符串内右侧的空格

shulandmimi

shulandmimi commented on May 7, 2019

@shulandmimi
function deleSpac(str,direction) { // 1 串的模板 2 清除哪边空格
            let Reg = '';
            switch(direction) {
                case 'left' : // 去除左边
                    Reg = /^[\s]+/g;
                    break;
                case 'right' : // 去除右边
                    Reg = /([\s]*)$/g;
                    break;
                case 'both' : // 去除两边
                    Reg = /(^\s*)|(\s*$)/g
                    break;
                default :   // 没传默认全部,且为下去除中间空格做铺垫
                    Reg = /[\s]+/g;
                    break;
            }
            let newStr = str.replace(Reg,'');
            if ( direction == 'middle' ){
                let RegLeft = str.match(/(^\s*)/g)[0]; // 保存右边空格
                let RegRight = str.match(/(\s*$)/g)[0]; // 保存左边空格
                newStr = RegLeft + newStr + RegRight; // 将空格加给清完全部空格后的字符串
            }
            return newStr;
        }
linghucq1

linghucq1 commented on May 8, 2019

@linghucq1
const str = '  s t  r  '

const POSITION = Object.freeze({
  left: Symbol(),
  right: Symbol(),
  both: Symbol(),
  center: Symbol(),
  all: Symbol(),
})

function trim(str, position = POSITION.both) {
  if (!!POSITION[position]) throw new Error('unexpected position value')
  
  switch(position) {
      case(POSITION.left):
        str = str.replace(/^\s+/, '')
        break;
      case(POSITION.right):
        str = str.replace(/\s+$/, '')
        break;
      case(POSITION.both):
        str = str.replace(/^\s+/, '').replace(/\s+$/, '')
        break;
      case(POSITION.center):
        while (str.match(/\w\s+\w/)) {
          str = str.replace(/(\w)(\s+)(\w)/, `$1$3`)
        }
        break;
      case(POSITION.all):
        str = str.replace(/\s/g, '')
        break;
      default: 
  }
  
  return str
}

const result = trim(str)

console.log(`|${result}|`) //  |s t  r| 
qqdnnd

qqdnnd commented on May 16, 2019

@qqdnnd
function trimStr(str, type) {
    const regObj = {
        left: /^\s+/,
        middle: /(^\s+)(\S)|\s+(\S)/g,
        right: /\s+$/,
        both: /(^\s+)|(\s+$)/g,
        all: /\s+/g
    };
    const reg = type && regObj[type] ? regObj[type] : regObj.both;
    const replaceStr = type === 'middle' ? (m, $1, $2, $3) => $1 ? m : $3 : '';
    return str.replace(reg, replaceStr);
}
trimStr('  aa bb  cc d d ee  ','middle');
tzjoke

tzjoke commented on May 16, 2019

@tzjoke
  • Regex: string.replace(/\s/g, '')
  • join: string.split(' ').join('')
yangchunboy

yangchunboy commented on May 27, 2019

@yangchunboy
var str = ' 1 2 3445 6    ';
console.log(str.split(' ').join('')) // 输出"1234456"

这样不是很简单吗 @haizhilin2013

Hxiaongrong

Hxiaongrong commented on May 28, 2019

@Hxiaongrong

function noSpace(str){
if (!str) return ''
return str.split('').filter(item => item !== ' ').join('')
}

Zhou-Bill

Zhou-Bill commented on Jun 4, 2019

@Zhou-Bill

" 123 56 ".replace(/\s+/g, "")

likeke1997

likeke1997 commented on Jun 5, 2019

@likeke1997
function trim(str) {
    return str.split(' ').join('');
}
var result = trim(' hello world, I am keke. ');
console.log(result); // helloworld,Iamkeke. 
myprelude

myprelude commented on Jun 13, 2019

@myprelude
 str.trim()
wanghaooo

wanghaooo commented on Jun 17, 2019

@wanghaooo

const trim = (str) => str.split('').filter((item) => item !== ' ').join('');

Damon99999

Damon99999 commented on Jun 17, 2019

@Damon99999

str.replace(/[ ]/g, "");

160 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mopig@haizhilin2013@zhoucumt@mosesyoung@JJL-SH

        Issue actions

          [js] 第2天 写一个方法去掉字符串中的空格 · Issue #6 · haizlin/fe-interview