java中string类_java中String类及方法以及案例

//String类的判断功能

/*

* boolean equals(Object obj)//判断字符串的值是否相等

* boolean equalsIgnoreCase(String str)//比较字符串的内容是否相同,忽略大小写

* boolean contains(String str)//判断大字符串中是否包含小字符串

* boolean startsWith(String str)//判断是否某某开头

* boolean endsWith(String str)//判断是否某某结束

* boolean isEmpty() //判断字符串内容是否为空

* */

public static void boolean_String(){

String str = "helloworld";

String str1 = "helloWorld";

String str2 = "helloworld";

String str3 = "";

String str4 = null;

System.out.println("equals:" + str.equals(str1));//false

System.out.println("equals:" + str.equals(str2));//true

System.out.println("equals:" + str.equalsIgnoreCase(str1));//true

System.out.println("equals:" + str.equalsIgnoreCase(str2));//true

System.out.println("contains:" + str.contains("ell"));//true

System.out.println("contains:" + str.contains("hw"));//false

System.out.println("startwith:" + str.startsWith("h"));//true

System.out.println("endswith:" + str.endsWith("d"));//true

System.out.println("isEmpty:" + str.isEmpty());

System.out.println("isEmpty:" + str3.isEmpty());

System.out.println("isEmpty:" + str4.isEmpty());//空指针异常,对象都不存在,所以不能调用方法

}

/*

* int length//获取长度

* char charAt(int index)//获取指定索引的字符

* int indexOf(int ch)//返回指定字符在此字符串中每一次出现处的索引

* //为什么是int类型,而不是char类型,原因是:'a'和97都可以代表‘a’

* int indexOf(String str)//返回指定字符串在此字符串中第一次出现处的索引

* int indexOf(int ch,int fromIndex)//返回指定字符在此字符串再次字符串从指定后第一次出现的索引

* int indexOf(String str,int fromIndex)//返回指定字符串在此字符串再次字符串从指定后第一次出现的索引

* String substring(int start)//从指定位置截取字符串,默认到末尾

* String substring(int start,int end)//从指定位置截取字符串,到指定位置结束

* */

public static void get_String(){

String str = "abcdefghijklmnopqrstuvwxyz";

System.out.println("字符串的长度为:"+str.length());

System.out.println("获取下标为6的字符:" + str.charAt(6));

System.out.println("返回o字符在字符串中出现的位置:" + str.indexOf('o'));

System.out.println("返回abc字符串在此字符串中第一次出现的位置:" + str.indexOf("abc"));

System.out.println("返回o字符在第5个字符以后出现的位置:" + str.indexOf('o',5));

System.out.println("返回opq字符串在地5个字符后出现的位置:" + str.indexOf("opq",5));

System.out.println("从第五个字符开始截取,到最后结尾:" + str.substring(5));

System.out.println("从第五个字符开始截取,到第十个字符结束:" + str.substring(5,10));

}

//遍历获取字符串中的每一个字符

public static void for_String(){

String str = "abcdeg";

for (int i = 0; i < str.length(); i++) {

System.out.println(str.charAt(i));

}

}

//对字符串中的字符进行排序

public static void sort_String(){

String str = "asdfghjkl";

char[] ch = str.toCharArray();

for (int i = 0; i < ch.length; i++) {

for (int j = 0; j < ch.length - 1; j++) {

if (ch[j] > ch[j + 1]) {

char temp = ch[j];

ch[j] = ch[j+1];

ch[j+1] = temp;

}

}

}

String str1 = String.valueOf(ch);

System.out.println(str1);

}

//给定一个字符串,统计有多少大写字母,有多少小写字母,以及数字

public static void compare_char(){

String str = "HLKJLKJfkfjsljfsklf123456&^**^";

int char_Lower = 0;

int char_Upper = 0;

int num = 0;

int others = 0;

for (int i = 0; i < str.length(); i++) {

if(str.charAt(i) > 'a' && str.charAt(i) < 'z'){

char_Lower++;

}else if(str.charAt(i) > 'A' && str.charAt(i) < 'Z'){

char_Upper++;

}else if(str.charAt(i) > '0' && str.charAt(i) < '9'){

num++;

}else {

others++;

}

}

System.out.println("小写子母有:" + char_Lower);

System.out.println("大写子母有:" + char_Upper);

System.out.println("数字有:" + num);

System.out.println("其他字符有:" + others);

}

/*

* String的转换功能

* byte[] getBytes()//转换为字节数组

* char[] tocharArray()//转换为字符数组

* static String valueOf(char[] chs)//把字符数组转成字符串

* static String valueOf(int i)//把int类型的数据转换为字符串

* //String类的valueOf方法可以把任意类型的数据转换成字符串

* String toLowerCase()//全部转换为小写字母

* String toUpperCase()//全部转换为大写字母

* String concat(String str)//拼接字符串

* */

public static void change_String(){

String str = "lakfashlfasf";

byte[] bytes = str.getBytes();//将字符串转换为字节数组

char[] chars = str.toCharArray();//将字符串转换为字符数组

String str1 = new String(bytes);//将字节数组转换为字符串

String str2 = String.valueOf(chars);//将字符数组转换为字符串

String Lower = str.toLowerCase();//将字符串中的字符转换为小写

String Upper = str.toUpperCase();//将字符串中的字符全部转换为大写

String str3 = "11111";

String str4 = str.concat(str3);//将字符串拼接

System.out.println("将字节数组转换为字符串:" + str1);

System.out.println("将字符数组转换为字符串:" + str2);

System.out.println("将字符串中的字符转换为小写:" + Lower);

System.out.println("将字符串中的字符转换为大写:" + Upper);

System.out.println("将字符串进行拼接:" + str4);

}

//将第一个字符变成大写,后面的字符变成小写

public static void First_Change() {

//繁琐的代码

String str = "helloWORLD";

// String str1 = str.substring(0,1);

// String str2 = str.substring(1);

// String s = str1.toUpperCase();

// String s1 = str2.toLowerCase();

// String s2 = s + s1;

// System.out.println(s2);

//优化,链式代码

String result = str.substring(0,1).toUpperCase().concat(str.substring(1).toLowerCase());

System.out.println(result);

}

/*

* 替换功能

* String replace(char old,char new)

* String replace(String old,String new)

* 去除字符串俩边空格

* String trim()

* 按字典顺序比较两个字符串

* int compareTo(String str)

* int compareToIgnoreCase(String str)

* */

public static void replace_String(){

String str = "陈梦茹";

String replace = str.replace('陈', '孔');

System.out.println("替换后的字符串:" + replace);

System.out.println("原先的字符串:" + str);

String replace1 = str.replace("陈梦茹", "哈");

System.out.println("替换后的字符串2:"+replace1);

String str1 = " afsklfj falkjfjsal ";

String trim = str1.trim();//去除字符串两边的空格

System.out.println("去除空格后的字符串:"+trim);

String[] split = str1.split(" +");

System.out.print("使用正则后的表达式:");

for (String string : split) {

System.out.print(string);

}

System.out.println();

String string1 = "acc";

String string2 = "aaa";

//区分大小写,返回的值为字符对应的数值相减

int i = string1.compareTo(string2);//如果前面的比后面的大返回一个正数,前面的比后面的小返回一个负数,相等返回0

System.out.println("比较:" + i);

String string3 = "aac";

String string4 = "ABC";

//不区分大小写

int i1 = string3.compareToIgnoreCase(string4);

System.out.println("比较:" + i1);

//特殊情况,如果一个字符串比另一个字符串长,而且短的字符串包含在长的字符串中,那么便返回的值为长度相减值

String string5 = "abcdefghij";

String string6 = "abc";

int i2 = string5.compareTo(string6);

System.out.println("特殊情况的比较:" + i2);

}

//按照指定要求拼接字符串

public static void concat_String(){

//给定一个数组

int [] nums = {1,2,3};

//要求输出的值为[1,2,3]

String string = "[";

String str = "";

str += string;

for (int i = 0; i < nums.length; i++) {

if(nums.length - 1 == i){

str += nums[i];

str += "]";

}else{

str += nums[i];

str += ", ";

}

}

System.out.println(str);

}

//字符串反转

public static void reverse_String(){

String string = "abc";

String str = "";

for (int i = string.length() - 1; i >= 0 ; i--) {

str += string.charAt(i);

}

System.out.println(str);

}

//判断java在字符串中出现了几次

public static void count_String(){

String string = "woaijavawozhendeaijavajavarangwokuaileheheda";

String string1 = "java";

int count = 0;

//旧版

// int i = string.indexOf(string1);

// while(i != -1){

// count++;

// int startIndex = i + string1.length();

// string = string.substring(startIndex);

// i = string.indexOf(string1);

// }

// System.out.println(count);

//改进版

int i;

while((i = string.indexOf(string1)) != -1){

count++;

string = string.substring(i + string1.length());

}

System.out.println(count);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值