在javascript中将字符串转换为句子大小写 [英] Convert string to sentence case in javascript

查看:125
本文介绍了在javascript中将字符串转换为句子大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望输入的字符串无论如何都应转换为句子。

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

喜欢


大家好,这是derp。谢谢大家回答我的问题。

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

转换为


大家好,这是derp。谢谢大家回答我的问题。

Hi all, this is derp. Thank you all to answer my query.


推荐答案

试试这个,它会对你有用。它也适用于具有前导空格的字符串

Try this, It will work fine for you. It will also work for String having leading spaces.

var string="hi all, this is derp. thank you all to answer my query.";
var n=string.split(".");
var vfinal=""
for(i=0;i<n.length;i++)
{
   var spaceput=""
   var spaceCount=n[i].replace(/^(\s*).*$/,"$1").length;
   n[i]=n[i].replace(/^\s+/,"");
   var newstring=n[i].charAt(n[i]).toUpperCase() + n[i].slice(1);
   for(j=0;j<spaceCount;j++)
   spaceput=spaceput+" ";
   vfinal=vfinal+spaceput+newstring+".";
 }
 vfinal=vfinal.substring(0, vfinal.length - 1);
 alert(vfinal);

这篇关于在javascript中将字符串转换为句子大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆