zoukankan      html  css  js  c++  java
  • Character类的2个定义大小写方法以及charAt(int index)方法

    API文档charAt(int index)是这样定义的:

    charAt(char index):Returns the char value at the specified index.在指定的索引返回字符的值;

    示例

    使用charAt函数获取字符串strCom中索引值为4的char值,并将结果赋值给int变量strLower:
                 String strCom = "I like you";
                 int strLower  = strCom.charAt(4);

    API文档isLowerCase是这样定义的:

    isLowerCase(char ch):Determines if the specified character is a lowercase character.确定如果指定的字符是一个小写字母开头;

     

    API文档isUpperCase是这样定义的:

    isUpperCase(char ch): Determines if the specified character is an uppercase character.确定如果指定的字符是一个大写字母开头。

    通过一个简单的小例子来运用;

    编写一个程序,要求输出一个字符串中大小写字母数以及其他字符数:

    一般的算法是:

    public class TestFinally {
         public static void main(String[] args) {
              int count1=0;int count2 = 0;int count3 = 0;
              String s = "jndhuf455NJKHJ455D";
              for (int i = 0;i<s.length();i++) {
                   char c = s.charAt(i);
                   if (c >= 'a' && c <= 'z') {
                          count1++;
                   } else if (c >= 'A' && c <= 'Z') {
                          count2++;
                   } else {
                          count3++;
                   }
              }
                       System.out.println(count1 + " - " + count2 + " - " + count3);
          }
     }

    输出结果:7 - 6 - 6

    使用isLowerCase以及isUppercase

    public class TestFinally {
         public static void main(String[] args) {
                  int count1=1;int count2 = 0;int count3 = 0;
                  String s = "jndhuf455NJKHJ455D";
                  for(int i = 0;i<s.length();i++) {
                       char c = s.charAt(i);

                       if (Character.isLowerCase(c)) {
                             count1++;
                       }
                       else if (Character.isUpperCase(c)) {
                             count2++;
                       }
                       else {
                             count3++;
                       }
                  }
                        System.out.println(count1 + " - " + count2 + "  - " + count3);
            }
    }

    运行输出结果:7 - 6 - 6



  • 相关阅读:
    ThreadLocal 详解
    外键的约束(Mysql、PostgreSQL)
    Linux命令中,$、#、@、0、1、2、*、?的作用
    $.ajax 方法参数总是记不住,在这里记录一下
    SVN提示https证书验证失败问题svn: E230001: Server SSL certificate verification failed:
    各类资源地址整合
    CentOS 7 上安装vim 解決 centos -bash: vim: command not found
    Beyond Compare 4提示已经过了30天试用期,破解方式,亲测可用
    Django 04 模板标签(if、for、url、with、autoeacape、模板继承于引用、静态文件加载)
    Django 03 模板路径、模板变量、常用的过滤器
  • 原文地址:https://www.cnblogs.com/wry13172/p/3582182.html
Copyright © 2011-2022 走看看