String类常用方法
2015-07-07 00:01
381 查看
一、常用方法1
public char charAt(int index) 返回字符串中第index个字符
public int length() 返回字符串的长度
public int indexOf(String str) 返回字符串中出现str的第一个位置
public int indexOf(String str, int fromIndex) 返回自字符串中从fromIndex开始出现str的第一个位置
public boolean equalsIgnoreCase(String another) 比较字符串与another是否一样(忽略大小写)
public String replace(char oldChar, char newChar) 在字符串中用newChar字符替换oldChar字符(全部替换哦)
public boolean startsWith(String str)
public boolean endsWith(String str)
public String toUpperCase()
public String toLowerCase()
public String subString(int beginIndex)
public String subString(int beginIndex, int endIndex)//注意第二个参数不是长度哦
public String trim()//返回将该字符串去掉开头和结尾空格后的字符串
二、常用方法2
1. 静态重载方法:
public static String valueOf(...)可以将基本类型数据转换为字符串;
2. public String[] split(String regex)可以将一个字符串按照指定的分隔符分隔,返回分割后的字符串数组
三、小应用:将字符串中的大写字母、小写字母、其他字符分别统计出来
public char charAt(int index) 返回字符串中第index个字符
public int length() 返回字符串的长度
public int indexOf(String str) 返回字符串中出现str的第一个位置
public int indexOf(String str, int fromIndex) 返回自字符串中从fromIndex开始出现str的第一个位置
public boolean equalsIgnoreCase(String another) 比较字符串与another是否一样(忽略大小写)
public String replace(char oldChar, char newChar) 在字符串中用newChar字符替换oldChar字符(全部替换哦)
public boolean startsWith(String str)
public boolean endsWith(String str)
public String toUpperCase()
public String toLowerCase()
public String subString(int beginIndex)
public String subString(int beginIndex, int endIndex)//注意第二个参数不是长度哦
public String trim()//返回将该字符串去掉开头和结尾空格后的字符串
二、常用方法2
1. 静态重载方法:
public static String valueOf(...)可以将基本类型数据转换为字符串;
2. public String[] split(String regex)可以将一个字符串按照指定的分隔符分隔,返回分割后的字符串数组
三、小应用:将字符串中的大写字母、小写字母、其他字符分别统计出来
public class StringDemo3 { public static void main(String[] args) { String s = "abcdDRGTS34fsgfsdTG98VFgaTBa"; method1(s); method2(s); method3(s); } public static void method1(String s) { int uCount = 0, lCount = 0, oCount = 0; for(int i = 0 ; i < s.length(); i++) { char c = s.charAt(i); if(c >= 'a' && c <= 'z') { lCount += 1; } else if(c >= 'A' && c <= 'Z') { uCount += 1; } else { oCount += 1; } } System.out.println("Lower count: " + lCount); System.out.println("Upper count: " + uCount); System.out.println("Other count: " + oCount); } public static void method2(String s) { int uCount = 0, lCount = 0, oCount = 0; String sL = "abcdefghijklmnopqrstuvwxyz"; String sU = sL.toUpperCase(); for(int i = 0 ; i < s.length(); i++) { char c = s.charAt(i); if(sL.indexOf(c) != -1) { lCount += 1; } else if (sU.indexOf(c) != -1) { uCount += 1; } else { oCount += 1; } } System.out.println("Lower count: " + lCount); System.out.println("Upper count: " + uCount); System.out.println("Other count: " + oCount); } public static void method3(String s) { int uCount = 0, lCount = 0, oCount = 0; for(int i = 0 ; i < s.length(); i++) { char c = s.charAt(i); if(Character.isLowerCase(c)) { lCount += 1; } else if (Character.isUpperCase(c)) { uCount += 1; } else { oCount += 1; } } System.out.println("Lower count: " + lCount); System.out.println("Upper count: " + uCount); System.out.println("Other count: " + oCount); } }
相关文章推荐
- GDI+绘制渐变色
- Java
- Java
- Java
- Java实战案例课程
- 安卓软件开发教程
- 【中服OA】OA系统在企业发展过程中发挥着不可或缺的作用
- 选择传统软件还是SaaS服务,企业考虑四个因素
- In our lifestyle have a lot of sorts
- If your are possible and in very good
- At the to get started with component
- Ladies is extremely sensitive when
- 企业社交软件:微信合适吗?
- 企业通讯软件助力企业实现高效办公
- 润普易度文控管理在IT企业中的应用
- 【php】【集基础】【namespace】
- php curl 及其参数
- 天津专业建站公司
- Django
- Installation of vim plugin --- YouCompleteMe