Convert string to sentence case in javascript

前端 未结 10 657
别那么骄傲
别那么骄傲 2020-12-03 19:19

I want a string entered should be converted to sentence case in whatever case it is.

Like

hi all, this is derp. thank you all to answer my qu

10条回答
  •  不知归路
    2020-12-03 20:07

    This is the solution I ended up using:

    str = 'hi all, this is derp. thank you all to answer my query.';
    temp_arr = str.split('.');
    for (i = 0; i < temp_arr.length; i++) {
    temp_arr[i]=temp_arr[i].trim()
    temp_arr[i] = temp_arr[i].charAt(0).toUpperCase() + temp_arr[i].substr(1).toLowerCase();
    }
    str=temp_arr.join('. ') + '.';
    return str;
    

提交回复
热议问题