确定字符串中第一个字母的大小写(上/下)-堆栈溢出 最近30次来自stackoverflow.com 2024-09-26T08:53:14Z https://stackoverflow.com/feeds/question/7824317 https://creativecommons.org/licenses/by-sa/4.0/rdf https://stackoverflow.com/q/7824317 确定字符串中第一个字母的大小写(大写/小写) 苏里亚·萨西达尔 https://stackoverflow.com/users/164002 2011年10月19日16:01:59Z 2011年10月21日下午5:57:30 <p>在web应用程序中,如何使用JavaScript确定给定字符串中的第一个字母是大写还是小写</p>(第页) https://stackoverflow.com/questions/7824317/-/7824376#7824376 1 通过taskinor回答或确定字符串中第一个字母的大小写(上/下) taskinoor公司 https://stackoverflow.com/users/377953 2011年10月19日16:06:26Z 2011年10月19日16:11:43Z <p>这只适用于英文字母表</p>(第页)<pre><code>var ch=myStr.chatAt(0);if(ch&gt;=“a”&amp;&amp!ch&lt;=“z”){//小的}else if(ch&gt;=“A”&amp;&amp!ch&lt;=“Z”){//资本}其他{//非英语字母字符}</code></pre> https://stackoverflow.com/questions/7824317/-/7824390#7824390 9 James Allardice关于确定字符串中第一个字母大小写(上/下)的回答 詹姆斯·阿拉迪斯 https://stackoverflow.com/users/790695 2011年10月19日16:07:17分 2011年10月19日16:35:58Z <p>您可以使用<code>来升级案例</code>:</p><pre><code>if(yourString.charAt(0)===yourString.charAt.(0).toUpperCase()){//大写!}</code></pre><p>如果你打算定期使用它,我建议把它放在String原型的函数中,如下所示:</p><pre><code>String.prototype.isFirstCapital=function(){return this.charAt(0)===this.charAt(0).toUpperCase();}if(yourString.isFirstCapital()){//大写!}</code></pre><p><strong>更新</strong>(基于评论)</p><p>我不知道在字符串不包含字母的情况下,您到底想做什么,但一个简单的解决方案是添加一个快速检查以查看它是否包含字母,如果没有,则返回false:</p><pre><code>String.prototype.isFirsCapital=函数(){return/^[a-z]/i.测试(this)&amp&amp;this.charAt(0)===this.charAt(0).toUpperCase();}</code></pre> https://stackoverflow.com/questions/77824317/-/7824406#7824406 0 Lapple对确定字符串中第一个字母大小写(上/下)的回答 褶皱 https://stackoverflow.com/users/642373 2011年10月19日16:08:47Z 2011年10月19日16:28:32Z <p>这将被递归调用,直到接近字符串中的第一个字母,否则将返回<code>“no letters”</code></p>(第页)函数getFirstCase(字符串){if(string==“”)返回“无字母”;var firstChar=字符串.charAt(0);/**如果同时使用小写和大写*是相等的,它不是一个字母*/if(firstChar.toLowerCase()===firstChar.toUpperCase(()){return getFirstCase(string.substr(1));}其他{return firstChar.toLowerCase()===firstChar?'小写':'大写';}}</code></pre><p>测试:</p><pre><code>console.log(getFirstCase('alphabet')),getFirstCase(“阳光”),获取第一个案例('123123'),getFirstCase(“@Hi”),getFirstCase('\nHAHA'));</code></pre> https://stackoverflow.com/questions/7824317/-/7824542#7824542 0 huMpty duMpty回答确定字符串中第一个字母的大小写(上/下) huMpty duMpty https://stackoverflow.com/users/925222 2011年10月19日16:18:45 Z 2011年10月19日16:18:45 Z <pre><code>var mystring=“测试字符串”;var first=“”;if(神秘){first=mystring[1];}如果(第一个){$('p').each(函数(){if($(this).text().charAt(0).toUpperCase()===${警报(“大写”);}});}</code></pre> https://stackoverflow.com/questions/7824317/-/7825284#7825284 0 nrabinowitz对确定字符串中第一个字母的大小写(上/下)的回答 纳比诺维茨 https://stackoverflow.com/users/380487 2011年10月19日17:15:30 Z 2011年10月19日17:15:30 Z <p>我很惊讶没有人提供regex解决方案-这似乎是迄今为止最简单的:</p><pre><code>函数getFirstCase(s){返回(/^[\d\W]*[A-Z]/)个测试?'上部':(/^[\d\W]*[a-z]/).测试?'下部':“无”;}</code></pre><p>公然窃取@Lapple的测试用例:</p><pre><code>console.log(getFirstCase('alphabet')),getFirstCase(“阳光”),获取第一个案例('123123'),getFirstCase(“@Hi”),getFirstCase('\nHAHA'));//下部上部无上部上部</code></pre><p>请参见<a href=“http://jsfiddle.net/nrabinowitz/a5cQa/“rel=”nofollow“>http://jsfiddle.net/nrabinowitz/a5cQa/</a></p>