API第一天 string方法 StringBuilder

目录

string 8方法

String.length()方法 获取字符串的长度(字符个数)

 String.trim()方法      trim修剪

 String.toUpperCase()/toLowerCase()方法 字符串英文部分转换大小写

 String.startsWith()方法 判断当前字符串是否是以给定的字符串开始的

 String.endsWith()方法 判断当前字符串是否是以给定的字符串结束的

 String.charAt()方法 返回指定位置(index)上的字符 返回值为char类型

  String.indexOf(String s)方法 检索给定字符串s在当前字符串中第一次出现的位置

  String.lastIndexOf(String s)方法 检索给定字符串s在当前字符串中最后出现的位置

   返回值为int类型 

String.substring(int start,int end)截取指定范围的字符串(含头不含尾)

静态方法valueOf(数据类型a) 将其他数据类型转换为String

StringBuilder

StringBuilder的常用方法

 getter setter


面试题

package APIday01;

public class StringDemo {
    public static void main(String[] args) {
        String s=new String("hello");//创建了2个对象
        String s1= "hello";//复用常量池中的字面量对象
        System.out.println("s:"+s);
        System.out.println("s1:"+s1);
        System.out.println(s==s1);//false ==比较的是地址是否相同
        System.out.println(s.equals(s1));
    }
}

用equals()方法比较字符串内容是否相同 s.equals(s1)

string 8方法

String.length()方法 获取字符串的长度(字符个数)

数组的length是属性 string的length是方法

public class LengthDemo {
    public static void main(String[] args) {
        String str="hello";
        System.out.println(str.length());
    }
}

 String.trim()方法      trim修剪

应用在 设置登录名时 去掉两边空格

public class TrimDemo {
    public static void main(String[] args) {
        String str="     hello world       ";
        System.out.println(str);
        str=str.trim();//去除两边空白的新的对象存储到str
        System.out.println(str);
    }

 String.toUpperCase()/toLowerCase()方法 字符串英文部分转换大小写

应用在输入验证码 将用户输入的全部转换为小写

public class UpperDemo {
    public static void main(String[] args) {
        String str="我爱Java";
        System.out.println(str.toUpperCase());//我爱JAVA 
        str=str.toLowerCase();
        System.out.println(str);//我爱java
    }

 String.startsWith()方法 判断当前字符串是否是以给定的字符串开始的

 String.endsWith()方法 判断当前字符串是否是以给定的字符串结束的

通过后缀名判断文件格式 .png .jpg

判断用boolean类型接收

public class StartsWithDemo {
    public static void main(String[] args) {
        String str="thinking in java";
        boolean starts=str.startsWith("think");
        System.out.println(starts);
        boolean ends=str.endsWith(".png");
        System.out.println(ends);
    }

 String.charAt()方法 返回指定位置(index)上的字符 返回值为char类型

 应用在根据下标生成字符

public class CharAtDemo {
    public static void main(String[] args) {
        String str="thinking in java";
        char c=str.charAt(9);//不包含下标
        System.out.println(c);
    }

  String.indexOf(String s)方法 检索给定字符串s在当前字符串中第一次出现的位置

  String.lastIndexOf(String s)方法 检索给定字符串s在当前字符串中最后出现的位置

   返回值为int类型 

 public static void main(String[] args) {
        String str="thinking in java";
        int index=str.indexOf("in");
        System.out.println(index);//2
        index=str.indexOf("in",3);//从下标为3的位置开始找
        //方法的重载
        System.out.println(index);//5
        index=str.indexOf("abc");
        System.out.println(index);//不存在显示-1

        index=str.lastIndexOf("in");
        System.out.println(index);
    }

String.substring(int start,int end)截取指定范围的字符串(含头不含尾)

 public static void main(String[] args) {
        String str="www.tedu.com";
        //如何把截取的写活?
        int start=str.indexOf(".")+1;
        int end=str.lastIndexOf(".");
        String name=str.substring(start,end);//截取下标4到7
        System.out.println(name);//tedu
        //重载
        name=str.substring(4);//从下标4一直截到最后
        System.out.println(name);
    }

静态方法valueOf(数据类型a) 将其他数据类型转换为String

public class ValueOf {
    public static void main(String[] args) {
        int a=123;
        String s1=String.valueOf(a);
        System.out.println(s1);

        double b=123.456;
        String s2=String.valueOf(b);
        System.out.println(s2);
        //方法二 和字符串相连 效率低
        String s3=b+" ";
        System.out.println(s3);
    }

StringBuilder

  • 由于string是不变对象,每次修改内容都会创建新的对象,因此string不适合频繁修改操作,为了解决这个问题,java提供了stringbuilder类
  • stringbuilder是专门用于修改字符串的,内部维护了一个可变的char数组,所做操作都是在这个数组上进行的,修改速度性能优秀,并且提供了修改字符串的常见方式:增 删 改 插
 //String中S大写  B大写
    public static void main(String[] args) {
        StringBuilder buider1=new StringBuilder();
        StringBuilder buider2=new StringBuilder("abc");
        //基于string来创建stringbuilder
        String str="abc";
        StringBuilder builder3=new StringBuilder(str);//abc串

        String str2=builder3.toString();//将builder3转换为string类型
    }

StringBuilder的常用方法

  • builder.append()-----------------------------追加内容
  • builder.delete() ----------------------------------删除内容
  • builder.replace(start,end,"替换字符") ------替换部分内容
  • builder.insert()------------------------------------插入内容

字符串内容需要查看,建议String---------实际应用中一般都是查看

字符串内容需要频繁修改,建议StringBuilder

 赋值getter 取值setter

public class GetterSetterDemo {
    private int x;
    private int y;

   public int getX(){
       return x;
   };

    public void setX(int x) {
        this.x = x;
    }
    public int getY(){
        return y;
    };
    public void setY(int y) {
        this.y = y;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值