zoukankan      html  css  js  c++  java
  • 第十四章

    1.字符串(String)的定义
    String str="内容";
    String str=new String(); //内容为null
    String str=new String("内容");
    2.java.lang.*
    字符串所在的包是java.lang.
    注意:所有在java.lang包下的类,我们使用时,不需要import。
    3.常用方法
    str.length();//字符串的长度
    str.indexOf(str1);//子串str1出现的下标。(从前往后)
    str.lastIndexOf(str1);//子串str1出现的下标。(从后往前)
    str.substring(begin);//截取从下表begin开始到末尾的子串。
    str.substring(begin,end);//截取下标begin开始到下标end的子串。
    str.trim();//去掉首尾空白
    str.toLowerCase();//返回字符串的小写
    str.toUpperCase();//返回字符串大写

    public class *** {
    public static void main(String[] agrs){
    Scanner console = new Scanner(System.in);
    String pwd = console.next();
    //str.lenght();//计算字符串长度
    int length = pwd.length();

    System.out.println(length);
    }
    }

    public class *** {
    public static void main(String[] agrs){
    String str = "adsadsdasdas";
    //indexOf (字符a):返回第一次出现字符a的下标
    //int index = str.indexOf('a');
    //indexOf (字符串):返回第一次出现字符串的下标
    //int index1 = str.indexOf("sa");

    //System.out.println(index);
    //System.out.println(index1);

    //从后往前找,返回第一次出现的下标
    int li = str.lastIndexOf('a');

    int li1 = str.lastIndexOf("sa");
    System.out.println(li);
    System.out.println(li1);

    }
    }

    public class *** {
    public static void main(String[] agrs){
    String str = "我是";
    String str1 = "好人";
    //"+"表示拼接
    String stra = str+str1;
    //字符串拼接
    String strc = str.concat(str1);

    System.out.println(stra);
    System.out.println(strc);
    }
    }

    public class ** {
    public static void main(String[] agrs){
    String str = "abcaaa";

    //截取从下标2开始到结尾的字符子串.
    String substr = str.substring(0);
    //截取从下标2开始到下标4的字符子串.包含前面不包含后面
    String substr1 = str.substring(0,1);
    System.out.println(substr);
    System.out.println(substr1);


    //str = " abc ";

    //System.out.println(str.length());
    //trim()去首尾空格
    //System.out.println(str.trim().length());
    }
    }

    public class ***{
    public static void main(String[] agrs){
    // //==:判断两个对象是否都是同一个对象
    // //equals比较内容是否相同
    //
    // String str = "abc";
    // String str1 = str;
    // String str2= new String("abc");
    // System.out.println(str2.equals(str1));
    // System.out.println(str==str2);

    String s1 = "abc";
    String s2 = "ABC";
    System.out.println(s1.equals(s2));
    //equalsIgnoreCase忽略大小写的比较
    System.out.println(s2.equalsIgnoreCase(s2));
    //toLowerCase转换成小写
    String s11 = s1.toLowerCase();
    System.out.println(s11);//abc
    //toUpperCase转换成大写
    String s12 = s1.toUpperCase();
    System.out.println(s12);//ABC
    }
    }

  • 相关阅读:
    关于求 p_i != i and p_i != i+1 的方案数的思考过程
    poj 3041 Asteroids 二分图最小覆盖点
    poj 1325 Machine Schedule 最小顶点覆盖
    poj 1011 Sticks 减枝搜索
    poj 1469 COURSES 最大匹配
    zoj 1516 Uncle Tom's Inherited Land 最大独立边集合(最大匹配)
    Path Cover (路径覆盖)
    hdu 3530 SubSequence TwoPoint单调队列维护最值
    zoj 1654 Place the Rebots 最大独立集转换成二分图最大独立边(最大匹配)
    poj 1466 Girls and Boys 二分图最大独立子集
  • 原文地址:https://www.cnblogs.com/abc199812/p/6970115.html
Copyright © 2011-2022 走看看