zoukankan      html  css  js  c++  java
  • SummerVocation_Learning--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 equalsIngnoreCase(String another),比较字符串与another是否一样。忽略大小写。

    public String replace(char oldChar, char newChar),在字符串中用newChar字符替换oldChar字符。

    举例:

     1 public class TestString {
     2 
     3     public static void main(String[] args) {
     4         String s1 = "sun java",s2 = "sun Java";
     5         System.out.println(s1.charAt(4)); //j
     6         System.out.println(s1.length()); //8
     7         System.out.println(s1.indexOf("java")); //4
     8         System.out.println(s1.indexOf("Java")); //-1
     9         System.out.println(s1.equals(s2)); //false
    10         System.out.println(s1.equalsIgnoreCase(s2)); //true
    11         
    12         String s = "sun orcle";
    13         String str = s.replace('s', 'S');
    14         System.out.println(str); //Sun java
    15         
    16     }
    17 
    18 }

    贰:

    public boolean startsWith(String prefix),判断字符串是否以prefix字符串开头。

    public boolean endsWith(String prefix),判断字符串是否以prefix字符串结尾。

    public String toUpperCase(),返回字符串的大写形式。

    public String toLowerCase(),返回字符串的小写形式。

    public String substring(int beginIndex),返回该字符串从beginIndex开始到结尾的子字符串。

    public String trim(),返回该字符串去掉开头和结尾空格后的字符串。

    举例:

     1 public class TestString {
     2 
     3     public static void main(String[] args) {
     4         String s = "Welcome to Beijing!";
     5         String s1 = " sun java ";
     6         System.out.println(s.startsWith("Welcome"));//true
     7         System.out.println(s.endsWith("Beijing"));//false,少了‘!'
     8         String sL = s.toLowerCase();
     9         String sU = s.toUpperCase();
    10         System.out.println(sL+"
    "+sU);
    11         String subs = s.substring(11);
    12         System.out.println(subs); //Beijing!
    13         String st = s1.trim();
    14         System.out.println(st); //sun java
    15     }
    16 
    17 }

    叁:

    public static String valueOf(***)可以将基本数据类型转换玩为字符串。

            如public static String valueOf(double d)

    public String[] split(String regex)可以将一个字符串按照指定的分隔符,返回分隔后的字符串数组。

    举例:

     1 public class TestString {
     2 
     3     public static void main(String[] args) {
     4         int n = 1234567;
     5         String sNumber = String.valueOf(n);
     6         System.out.println("n is the "+sNumber.length()+"-digit number");
     7         //n is the 7-digit number
     8         String s = "Mary,F,1992";
     9         String[] s2 = s.split(",");
    10         for (int i = 0; i < s2.length; i++) {
    11             System.out.println(s2[i]);
    12         }
    13         /*
    14          * Mary
    15          * F
    16          * 1992
    17          */
    18     }
    19 
    20 }
  • 相关阅读:
    light oj 1105 规律
    light oj 1071 dp(吃金币升级版)
    light oj 1084 线性dp
    light oj 1079 01背包
    light oj 1068 数位dp
    light oj 1219 树上贪心
    light oj 1057 状压dp TSP
    light oj 1037 状压dp
    矩阵快速幂3 k*n铺方格
    矩阵快速幂2 3*n铺方格
  • 原文地址:https://www.cnblogs.com/DSYR/p/9292656.html
Copyright © 2011-2022 走看看