String类

创建字符串的常用3+1种方式:
    三种构造方法:
    public String();创建一个空白字符串,不含有任何内容。
    public String(char[] array);根据字符数组的内容创建对应字符串
    public String(byte[] array);根据字节数组的内容创建对应字符串
    一种直接创建
    String str="Hello,java!";

equals方法

package demo01;
/*
==是进行地址值的比较,如果确实需要字符串的内容比较,可以使用两个方法:
public boolean equals(Object obj):参数可以是任何对象
只有参数是一个字符串并且内容相同才会给true,否则给false。
注意事项:
1.任何对象都能被Object进行接收。
2.equals方法具有对称性 str1.equals(str2)与str2.equals(str1)结果一样;
3.如果比较双方一个常量一个变量推荐吧常量写在前面
public boolean equalsIgnoreCase(String str)
忽略大小写进行比较
 */
public class DemoString {
    public static void main(String[] args) {
        String str1="hello";
        String str2="hello";
        char[]charArray={'h','e','l','l','o'};
        String str3=new String(charArray);
        System.out.println(str1.equals(str2));  //true
        System.out.println(str2.equals(str3));  //true
        System.out.println(str3.equals(str1));  //true
        System.out.println(str1.equals("hello"));  //true
        String str4="Hello";
        System.out.println(str1.equals(str4));  //false
        System.out.println(str1.equalsIgnoreCase(str4));  //true
    }
 }

 String中与获取相关的常用方法

package demo01;
/*
String中与获取相关的常用方法
public int length(); 获取当前字符串的字符个数,拿到当前字符串的长度。
public  String concat(String str);将当前字符串和参数字符串拼接为返回值新的字符串
public char charAt(int index);获取指定索引位置的单个字符
public int indexOf(String str);查找参数字符串在本字符串中第一次出现的索引位置,如果没有返回值-1。
 */
public class Demo02StringGet {
    public static void main(String[] args) {
        int length = "hduiewfriuejfhnkjefnefiff".length();
        System.out.println("字符串的长度是" + length);
        String str1 = "我爱你";
        String str2 = "一生一世";
        String str3 = str1.concat(str2);
        System.out.println(str1 );
        System.out.println(str2 );
        System.out.println(str3 );
        char c = "hello".charAt(2);
        System.out.println("在二号索引位置的字符是"+c);
         String original="HelloWorld";
        int oll = original.indexOf("oll");
        System.out.println("第一次出现的索引位置是"+oll);
    }
}
字符串的截取方法:
public String subString(int index);
截取从参数位置一直到字符串末尾,返回新的字符串。
public String substring(int begin,int end):截取从begin开始到end结束,中间的字符串
注意:左闭右开,包含左边,不包含右边。
package demo01;
/*
字符串的截取方法:
public String subString(int index);
截取从参数位置一直到字符串末尾,返回新的字符串。
public String substring(int begin,int end):截取从begin开始到end结束,中间的字符串
注意:左闭右开,包含左边,不包含右边。
 */
public class Demo03Substring {
    public static void main(String[] args) {
        String str1="HelloWorld";
        String str2=str1.substring(5);
        System.out.println(str1);
        System.out.println(str2);
        String str3=str1.substring(3,6);
        System.out.println(str3);
    }
}
String当中与转换相关的常用方法有
public char[] toCharArray():将当前字符串拆分为字符数组作为返回值。
public byte[] getBytes():获得当前字符串底层的字节数组。
public String replace(CharSequence oldString,CharSequence newString)
将所有出现的老字符串,替换为新的字符串,返回替换之后的新字符串。
备注:CharSequence,意思就是可以接受字符串类型。
package demo01;
/*
String当中与转换相关的常用方法有
public char[] toCharArray():将当前字符串拆分为字符数组作为返回值。
public byte[] getBytes():获得当前字符串底层的字节数组。
public String replace(CharSequence oldString,CharSequence newString)
将所有出现的老字符串,替换为新的字符串,返回替换之后的新字符串。
备注:CharSequence,意思就是可以接受字符串类型。
 */
public class Demo04StringConvert {
    public static void main(String[] args) {
        //转换成为字符数组
        char[] chars = "HelloWorld!".toCharArray();
        System.out.println(chars[5]);
        System.out.println(chars.length);
        //转换成为字节数组
        byte[] bytes = "abc".getBytes();
        System.out.println(bytes[0]);
        System.out.println(bytes.length);
        //新旧字符串的替换
        String str1="How do you do ";
        System.out.println(str1);
        String replace = str1.replace("o", "0");
        System.out.println(replace);

    }
}
分割字符串的方法:
public String[] split(String regex)按照参数的规则将字符串切分为若干个部分。
注意事项split方法的参数不其实是一个正则表达式
按照英文据句点切分需要写成"\\."
package demo01;
/*
分割字符串的方法:
public String[] split(String regex)按照参数的规则将字符串切分为若干个部分。
注意事项split方法的参数不其实是一个正则表达式
按照英文据句点切分需要写成"\\."

 */
public class Demo05StringSplit {
    public static void main(String[] args) {
        String str1="aaa bbb ccc ddd";
        String[] split = str1.split(" ");
        for (int i = 0; i < split.length; i++) {
            System.out.println(split[i]);
        }
        String replace = str1.replace(" ", ",");
        String[] split1 = replace.split(",");
        for (int i = 0; i < split1.length; i++) {
            System.out.println(split1[i]);
        }
        String str3="aaa.bbb.ccc";
        String[] split2 = str3.split("\\.");
        for (int i = 0; i < split2.length; i++) {
            System.out.println(split2[i]);
        }
    }
}
题目:定义一个方法,将数组{1,2,3}按照指定格式拼接成一个字符串[world1#world2#world3]
package demo01;
/*
题目:定义一个方法,将数组{1,2,3}按照指定格式拼接成一个字符串[world1#world2#world3]
分析题目:
定义一个数组:int[] array={1,2,3};
定义一个方法:public static String formArrayToString[array];
方法三要素
返回值类型:String
方法名称:fromArrayToString
参数:int[]
方法内容:for循环,字符串拼接,if/else判断是否为数组最后一个元素。
 */
public class Demo06StringPractise {
    public static void main(String[] args) {
        int[] array={1,2,3};
        String result=formArrayToString(array);
        System.out.println(result);
    }
    public static  String formArrayToString(int[]array){
        String str="[";
        for (int i = 0; i < array.length; i++) {
            if (i==array.length-1){
                str += "world"+array[i]+"]";
            }else{
                str += "world"+array[i]+"#";
            }

        }
        return str;
    }



}
题目:键盘输入一段字符串,并且统计其中各种字符出现的字数
package demo01;

import java.sql.SQLOutput;
import java.util.Scanner;

/*
题目:键盘输入一段字符串,并且统计其中各种字符出现的字数
解题思路:
1.键盘输入。调用Scanner方法;
2.创建四个变量用来统计四种不同的字符类型;
3.将键盘输入的字符串转换为char型数组
4.遍历数组并进行判断统计。
 */
public class Demo07StringCount {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入字符串");
        String input=sc.next();  //获取键盘输入的一个字符串
        int countUpper=0;  //大写字母
        int countLower=0;  //小写字母
        int countNumber=0;  //数字
        int countOther=0;   //其他字符

        char[] charArray=input.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            char ch=charArray[i];  //当前单个字符
            if ('A' <= ch && ch <= 'Z'){
                countUpper++;
            }else if('a' <= ch && ch <= 'z'){
                countLower++;
            }else if('0' <= ch && ch <='9'){
                countNumber++;
            }else{
                countOther++;
            }

        }
        System.out.println("大写字母有"+countUpper);
        System.out.println("小写字母有"+countLower);
        System.out.println("数字字符有"+countNumber);
        System.out.println("其他字符有"+countOther);
    }
}

java.util.Arrays是一个与数组相关的工具类,里面提供了大量静态方法,用来实现数组常见操作。

public static String toString(数组):将参数数组变成字符串(按照默认格式[元素1 ,元素2 ])
public static void sort(数组) 按照默认升序对数组的元素进行排序。
注意事项:
1.如果是数值,sort默认升序从小到大
2.如果是字符串,sort默认按字母升序
3.如果是自定义的类型,name这个自定义的类需要有(Comparable或者Comparator接口支持)
package demo04;

import java.util.Arrays;

/*
 java.util.Arrays是一个与数组相关的工具类,里面提供了大量静态方法,用来实现数组常见操作。

 public static String toString(数组):将参数数组变成字符串(按照默认格式[元素1 ,元素2 ])
 public static void sort(数组) 按照默认升序对数组的元素进行排序。
 注意事项:
 1.如果是数值,sort默认升序从小到大
 2.如果是字符串,sort默认按字母升序
 3.如果是自定义的类型,name这个自定义的类需要有(Comparable或者Comparator接口支持)

  */
public class Demo01Arrays {
     public static void main(String[] args) {
         int[] intArray={80,20,50,40,100};
         //将int数组按照默认格式变成字符串
         String intStr = Arrays.toString(intArray);
         System.out.println(intStr);
         Arrays.sort(intArray);
         System.out.println(Arrays.toString(intArray));
     }
}
 
java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作。
public static double abs(double num)获取绝对值
public static double ceil(double num)向上取整数
public static double floor(double num)向下取整
public static long round (double num)四舍五入
package demo04;
/*
java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作。
public static double abs(double num)获取绝对值
public static double ceil(double num)向上取整数
public static double floor(double num)向下取整
public static long round (double num)四舍五入
 */
public class Demo03Math {
    public static void main(String[] args) {
        //获取绝对值
        System.out.println(Math.abs(-20));
        //向上取整
        System.out.println(Math.ceil(20.5));
        //向下取整
        System.out.println(Math.floor(25.6));
        //四舍五入
        System.out.println(Math.round(10.1));
        System.out.println(Math.round(15.6));
    }
}
题目:请使用Arrays相关的API,将一个随机字符串中的字符升序排列,并倒序打印
package demo04;
/*
题目:请使用Arrays相关的API,将一个随机字符串中的字符升序排列,并倒序打印
 */
import java.util.Arrays;

public class Demo02ArraysPractise {

    public static void main(String[] args) {
        String str="sfhibrfhn";
        //将字符串转化为一个char型数组
        char[] chars = str.toCharArray();
        //进行升序排列
        Arrays.sort(chars);
        //进行倒序输出
        for (int i = chars.length-1; i >0 ; i--) {
            System.out.println(chars[i]);
            
        }



    }
}
题目:
计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数
package demo04;
/*
题目:
计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数
分析及思路:
1.既然已经确定了范围那就用for循环
2.起点位置-10.8应该转换为-10
    2.1用int进行强转
    2.2用Math.ceil进行强转
3.每一个数字都是整数,所以应该用步进表达式num++;
4.拿到绝对值用Math.abs方法
5.进行累加的到结果
 */
public class Demo04MathPractise {
    public static void main(String[] args) {
        int count=0;
        double min=-10.8;
        double max=5.9;
        for (int i=(int)min;i<max;i++){
            int abs=Math.abs(i);
            if(abs>6||abs<2.1){

                count++;
            }
        }
        System.out.println("总共个数有"+count);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值