zoukankan      html  css  js  c++  java
  • string类常用方法

    package cn.mc;  
    public class StringTestMc {  
     private String str = "helloWorld";  
     /** 
      * 将字符串变成一个字符数组 
      */  
     public void tocharyArry() {  
      char c[] = str.toCharArray();  
      for (int i = 0; i < c.length; i++) {  
       System.out.println("转为数组输出:" + c[i]);  
      }  
     }  
     /** 
      * 从字符串中取出指定位置的字符 
      */  
     public void tocharAt() {  
      char c = str.charAt(3);  
      System.out.println("指定字符为:" + c);  
     }  
     /** 
      * 将字符串变成一个byte数组 
      */  
     public void tobyte() {  
      byte b[] = str.getBytes();  
      System.out.println("转换成byte数组输出为:" + new String(b));  
     }  
     /** 
      * 取得一个字符串的长度 
      */  
     public void tolength() {  
      int l = str.length();  
      System.out.println("这个字符串的长度为:" + l);  
     }  
     /** 
      * 查找一个指定的字符串是否存在,返回的是字符串的位置,如果不存在,则返回-1 
      */  
     public void toindexOf() {  
      int a1 = str.indexOf("e");// 查找字符e的位置  
      int a2 = str.indexOf("l", 2);// 查找l的位置,从第3个开始查找  
      System.out.println("e的位置为:" + a1);  
      System.out.println("l的位置为:" + a2);  
     }  
     /** 
      * 去掉字符串左右空格 
      */  
     public void totrim() {  
      String str1 = "       hello         ";  
      System.out.println("去掉左右空格后输出:" + str1.trim());  
     }  
     /** 
      * 字符串的截取 
      */  
     public void tosubstring() {  
      System.out.println("截取后的字符为:" + str.substring(0, 3));// 截取0-3个位置的内容  
      System.out.println("从第3个位置开始截取:" + str.substring(2));// 从第3个位置开始截取  
     }  
     /** 
      * 按照指定的字符串拆分字符,拆分的数据将以字符串数组的形式返回 
      */  
     public void tosplit() {  
      String s[] = str.split("e");// 按hello中的e进行字符串拆分  
      for (int i = 0; i < s.length; i++) {  
       System.out.println("拆分后结果为:" + s[i]);  
      }  
     }  
     /** 
      * 将字符串进行大小写转换 
      */  
     public void tochange() {  
      System.out.println("将"hello"转换成大写为:" + str.toUpperCase());// 将hello转换成大写  
      System.out.println("将"HELLO"转换成大写为:"  
        + str.toUpperCase().toLowerCase());// 将HELLO转换成小写  
     }  
     /** 
      * 判断是否以指定的字符串开头或者结尾 
      */  
     public void tostartsWithOrendWith()  
     {  
      if(str.startsWith("he"))//判断字符串是否以he开头  
      {  
       System.out.println("字符串是以he开头");  
      }  
      if(str.endsWith("lo"))  
      {  
       System.out.println("字符串是以lo结尾");  
      }   
     }  
     /** 
      * 两个String类型内容比较 
      */  
     public void toequals()  
     {  
      String str3="world";  
      if(str.equals(str3))  
      {  
       System.out.println("这俩个String类型的值相等");  
      }  
      else  
       System.out.println("这俩个String类型的不值相等");  
     }  
     /** 
      * 两个字符串不区分大小写进行比较 
      */  
     public void toequalslgnoreCase()  
     {  
      String str4="HELLO";  
      if(str.equalsIgnoreCase(str4))  
      {  
       System.out.println("hello和HELLO忽略大小写比较值相等");  
      }  
     }  
     /** 
      * 将一个指定得到字符串替换成其他字符串 
      */  
     public void toreplaceAll()  
     {  
      String str5=str.replaceAll("l", "a");  
      System.out.println("替换后的结果为:"+str5);  
     }  
     public static void main(String[] args) {  
     StringTest obj = new StringTest();  
      obj.tocharyArry();  
      obj.tocharAt();  
      obj.tobyte();  
      obj.tolength();  
      obj.toindexOf();  
      obj.totrim();  
      obj.tosubstring();  
      obj.tosplit();  
      obj.tochange();  
      obj.tostartsWithOrendWith();  
      obj.toequals();  
      obj.toequalslgnoreCase();  
      obj.toreplaceAll();  
     }  
    }  
    ---------------------
    作者:loverXbc
    来源:CSDN
    原文:https://blog.csdn.net/melodysky/article/details/51967462
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    sp2010 升级sp2013 用户无法打开网站
    powerviot install in sharepoint 2013
    can not connect cube in performancce dashboard
    westrac server security configure user info
    添加报表服务在多服务器场
    sharepoint 2013 office web app 2013 文档在线浏览 IE11 浏览器不兼容解决方法
    delete job definition
    目前付款申请单内网打开慢的问题
    item style edit in sharepoint 2013
    Could not load file or assembly '$SharePoint.Project.AssemblyFullName$'
  • 原文地址:https://www.cnblogs.com/tyadmin/p/11241451.html
Copyright © 2011-2022 走看看