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

问题描述

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

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

喜欢

转换为


推荐答案

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

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中将字符串转换为句子大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:30
查看更多