zoukankan      html  css  js  c++  java
  • JAVA String 类

    java String类中的常用方法:
    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 prefix)
    判断字符串是否以prefix字符串开头;
    public boolean endsWith(String suffix)
    判断一个字符串是否以suffix字符串结尾;
    public String toUpperCase()
    返回一个字符串为该字符串的大写形式;
    public String toLowerCase()
    返回一个字符串为该字符串的小写形式
    public String substring(int beginIndex)
    返回该字符串从beginIndex开始到结尾的子字符串;
    public String substring(int beginIndex,int endIndex)
    返回该字符串从beginIndex开始到endsIndex结尾的子字符串
    public String trim()
    返回该字符串去掉开头和结尾空格后的字符串
    public String[] split(String regex)
    将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组

    代码示例:

    public class StringDemo {

     public static void main(String[] args) {
      String str=new String("hello world");
      System.out.println(str);
      System.out.println(str.charAt(2));
      System.out.println(str.indexOf("o"));
      System.out.println(str.indexOf("o", 5));
      System.out.println(str.equalsIgnoreCase("HELLO WORLD"));
      System.out.println(str.replace('o', 'O'));
      System.out.println(str.startsWith("hello"));
      System.out.println(str.endsWith("WORLD"));
      System.out.println(str.toUpperCase());
      System.out.println(str.toLowerCase());
      System.out.println(str.substring(1));
      System.out.println(str.substring(1, 9));
      System.out.println(str.trim());
      System.out.println(str.split(" "));
       }

    }

    运行结果如下:

    hello world
    l
    4
    7
    true
    hellO wOrld
    true
    false
    HELLO WORLD
    hello world
    ello world
    ello wor
    hello world
    [Ljava.lang.String;@2a139a55

  • 相关阅读:
    第一章 简介(待续)
    第十六章 漫话网站架构师(待续)
    第十五章 网站架构师职场攻略(待续)
    第十四章 架构师领导艺术(待续)
    第十三章 大型网站典型故障分析案例(待续)
    上帝造题的七分钟2/花神游历各国/GSS4 线段树维护区间开方 By cellur925
    LuoguP1606 [USACO07FEB]荷叶塘Lilypad Pond 【最短路】By cellur925
    NOIp2013 车站分级 【拓扑排序】By cellur925
    NOI题库--盒子和小球系列 By cellur925
    关于对动态规划的思考 【转】
  • 原文地址:https://www.cnblogs.com/yfz1552800131/p/5471020.html
Copyright © 2011-2022 走看看