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

    一、常用方法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 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);
        }
    
    }
  • 相关阅读:
    [BZOJ4034][HAOI2015]树上操作
    [BZOJ1030][JSOI2007]文本生成器
    [BZOJ2763][JLOI2011]飞行路线
    [POJ3667]Hotel
    [codevs1566]染色
    [codevs2460]树的统计
    [BZOJ2667][cqoi2012][kcoj]模拟工厂
    [NOI2009][codevs1846]KCOJ0191]植物大战僵尸
    [POJ1087]A Plug for UNIX
    Educational Round 66 题解
  • 原文地址:https://www.cnblogs.com/byron0918/p/4625775.html
Copyright © 2011-2022 走看看