java基础学习

1.利用Scanner类输入

import java.util.Scanner;

1.整型输入

Scanner scanner=new Scanner(System.in);
int a=scanner.nextInt();

2.浮点型输入

 Scanner Scanner =new Scanner(System.in);
 double sum= Scanner.nextDouble();

3.字符串输入

 Scanner Scanner = new Scanner(System.in);
 String str1 = Scanner.next();

4.单个字符输入

 Scanner scanner = new Scanner(System.in);
 char num1 =scanner.next().charAt(0);
2.String类的初始化
public class Main {
    public static void main(String[] args) {
        //创建一个空字符串
        String str1=new String();
        //创建一个内容为abcd的字符串
        String str2 = new String("abcd");
        //字符数字
        char[] charshuz=new char[]{'2','4','5','1'};
        //创建一个内容为字符数组的字符串
        String str3=new String(charshuz);
        System.out.println(str1);
        System.out.println(str2);
        //输出字符数组
        System.out.println(charshuz);
        System.out.println(str3);
    }
}

3.字符串的获取功能
public class Main {
    public static void main(String[] args) {
     String s="huhuhfwef";
        System.out.println("字符串的长度为:"+s.length());
        System.out.println("字符串的第一个字符:"+s.charAt(0));
        System.out.println("字符h第一次出现的位置:"+s.indexOf('h'));
        System.out.println("字符h最后一次出现的位置:"+s.lastIndexOf('h'));
        System.out.println("字符串hu第一次出现的位置:"+s.indexOf("hu"));
        System.out.println("字符串hu最后一次出现的位置"+s.lastIndexOf("hu"));
        System.out.println("索引为5的字符:"+s.charAt(5));
    }
}

4.字符串的转换操作
public class Main {
    public static void main(String[] args) {
     String str="AbCd";
     //将字符串转为字符数组
        char[] array = str.toCharArray();
        //输出字符数组
        System.out.print("将字符串转化为字符数组的结果是:");
        for(int i=0;i<array.length;i++){
            System.out.print(array[i]+" ");
        }
        //各种类型的数据均可以
        System.out.println("\n将int值转化为字符串之后的结果:"+String.valueOf(12));
        System.out.println("将字符转化为大写字母之后的结果:"+str.toUpperCase());
        System.out.println("将字符串转化为小写字母之后的结果:"+str.toLowerCase());

    }
}

5.字符串的替换和去除空格操作
public class temp {
    public static void main(String[] args) {
        String s1="isme";
        System.out.println("将is替换成is't的结果是:"+s1.replace("is","is't"));
        String s2=" f f w a d   ";
        System.out.println("去除s2两端空格后的结果是:"+s2.trim());
        System.out.println("去除s2字符串中的所有空格后的结果是:"+s2.replace(" ",""));
    }
}

6.字符串的判断操作

public class temp {
    public static void main(String[] args) {
       String s1="hello word";
       String s2="llo";
        System.out.println("判断s1是否以he开头:"+s1.startsWith("he"));
        System.out.println("判断字符串是否以ord结尾"+s1.endsWith("ord"));
        System.out.println("判断是否包含s1是否包含llo:"+s1.contains("llo"));
        System.out.println("判断s1是否为空:"+s1.isEmpty());
        System.out.println("判断两个字符是否相等:"+s1.equals(s2));
    }
}

7.字符串截取和分割
public class temp {
    public static void main(String[] args) {
       String s1="好好学习-天天向上-加油";
        System.out.println("从第五个字符截取到末尾的结果:"+s1.substring(4));
        System.out.println("从第五个字符截取到第六个字符的结果:"+s1.substring(4,6));

        System.out.print("分割后的字符串数组元素依次为:");
        String [] arr=s1.split("-");
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }

    }
}

8.StringBuffer类

字符串是常量,一旦创建长度和内容无法修改,除非创建新的字符串

StringBuffer类解决了此问题

1.添加操作

public class temp {
    public static void main(String[] args) {
      //创建一个字符串缓冲区
       StringBuffer s = new StringBuffer();
       //添加元素
       s.append("jash");
       System.out.println("append()添加后结果为:"+s);
       //在指定位置添加字符串
        s.insert(2,"123");
        System.out.println("insert()插入后结果为:"+s);


    }

    }

2.删除操作


public class temp {
    public static void main(String[] args) {
      //创建一个字符串缓冲区
       StringBuffer s = new StringBuffer("eifhwei");
       //指定删除范围
        s.delete(1,3);
        System.out.println("删除后结果为:"+s);
        //指定位置删除
        s.deleteCharAt(2);
        System.out.println("删除后结果为:"+s);
        //清空缓冲区
        s.delete(0,s.length());
        System.out.println("缓冲区:"+s);
    }

    }

 

3.修改操作 


public class temp {
    public static void main(String[] args) {
      //创建一个字符串缓冲区
       StringBuffer s = new StringBuffer("helloword!!!");
       s.setCharAt(1,'8');
        System.out.println("修改指定位置字符后的结果为:"+s);
        s.replace(1,4,"842232");
        System.out.println("替换指定位置字符(串)的结果是:"+s);
        s.reverse();
        System.out.println("字符串纺专结果为:"+s);

    }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值