zoukankan      html  css  js  c++  java
  • java字符串大小写转换的两种方法


    转载自:飞扬青春sina blog
    java字符串大小写转换的两种方法
     
    import java.io..*
     
    public class convertToPrintString
     {   
           public static void main(String[] args) throws IOException
           {
                InputStreamReader reader = new InputStreamReader(System.in); 
                BufferedReader input = new BufferedReader(reader); 
                System.out.print("Please enter your word:"); 
                String text =  input.readLine(); 
                String s = convertString(text); 
                System.out.println(s);
            }
            //第一种方法
           public static String convertString(String src)
           {
                char[] array = src.toCharArray();
                 int temp = 0;
                 for (int i = 0; i < array.length; i++)
                 {
                     temp = (int) array[i];
                     if (temp <= 90 && temp >= 65)
                     { // array[i]为大写字母
                         array[i] = (char) (temp + 32);
                     } else if (temp <= 122 && temp >= 97)
                               { // array[i]为小写字母
                                    array[i] = (char) (temp - 32);
                     }
                  }
                  return String.valueOf(array);
              }
            
              //第二种方法
               public static String convertString(String str)
               {
                   String upStr = str.toUpperCase(); 
                   String lowStr = str.toLowerCase(); 
                   StringBuffer buf = new StringBuffer(str.length()); 
                   for(int i=0;i
                   { 
                      if(str.charAt(i)==upStr.charAt(i))
                      { 
                          buf.append(lowStr.charAt(i)); 
                      }
                     else
                     {
                         buf.append(upStr.charAt(i)); 
                      } 
                    } 
                    return   buf.toString(); 
                 }
     }

  • 相关阅读:
    python笔记之发送邮件
    Python基础
    Python操作csv文件
    Python之操作Excel、异常处理、网络编程
    Python操作数据库及hashlib模块
    函数、装饰器、模块
    Python之三元运算、集合、函数
    python基础--字符串操作、列表、元组、文件操作
    抓包工具Fiddler及Charles
    input type=number maxlength无效
  • 原文地址:https://www.cnblogs.com/jixiegongdi/p/4686928.html
Copyright © 2011-2022 走看看