Java常用字符串方法小结
程序员文章站
2024-03-02 09:27:46
下面是对字符串操作的代码小总结。大部分是string类的 操作方法,需要的朋友可以参考下
public class studystring {
publ...
下面是对字符串操作的代码小总结。大部分是string类的 操作方法,需要的朋友可以参考下
public class studystring { public static void main(string[] ergs){ //字符串的声明与赋值 string name = "蔡宇飞"; string hisname = new string ("小明"); system.out.println(name+"和"+hisname+"是好朋友"); //字符串基本操作 //获取字符串的长度 //字符串名.length() 返回字符的个数 string hello = "hello world!"; int length = hello.length(); system.out.println(hello+"的长度是"+length); //字符串连接 //string类提供的concat()方法 //字符串1.concat(字符串2) 返回值是一个字符串 string twoname = name.concat(hisname); system.out.println(twoname); //字符串比较 //string提供的equals()方法,返回值为boolean类型。两个字符串中每个字符完全一致时才为turn.否则为false //字符串1.equals(字符串2) string str1 = "fuck"; string str2 = "fuck"; if (str1.equals(str2)) system.out.println("相同"); else system.out.println("不同"); //string还提供了equalsignorecase()方法,这个与上面的区别是不区分字母的大小写。返回值同样为boolean类型 //字符串1.equalsignorecase(字符串2) if(str1.equalsignorecase(str2)) system.out.println("相同"); else system.out.println("不同"); //字符串截取 //从字符串中截取一部分作为新的字符串,string类提供的substring来实现 //字符串.substring (开始位置); 或者 字符串.substring (开始位置,结束位置); //第一种是从开始位置直到结束,第二种从开始位置到结束位置. string my ="my name is caiyufei,i love java and python."; string love =my.substring(20); string myname = my.substring(11, 19); system.out.println(love); system.out.println(myname); //字符串查找 //在一个字符串中查找另一个字符串,string类提供了indexof方法来实现 //字符串1.indexof(字符串2) 或 字符串1.indexof(字符串2,开始位置) int lovenum = my.indexof(love); int mynamenum = my.indexof(myname); system.out.println(lovenum); system.out.println(mynamenum); //字符串替换 //用一个新字符去替换字符串中指定的所有字符 string类提供了replace方法实现这种替换 //字符串1.replance(被替换字符,替换字符) char i_ = 'i'; char m_ = 'm'; system.out.println(love.replace(i_, m_)); //m love java and python. //字符串与字符数组 //将字符数组作为构造函数的参数直接转换成字符串 char [] helloarray = {'h','e','l','l','o'}; string hellostring = new string (helloarray); system.out.println(hellostring); //将字符串转为字符数组 //tochararray()方法 char [] array = hellostring.tochararray(); for (int i = 0;i<array.length;i++) system.out.println(array[i]); //其他方法 //字符串中英文字母转换为小写 string eng = "i love english"; string eng_1 = eng.tolowercase(); system.out.println(eng_1); //字符串中英文字母转为大写 string eng_2 = eng.touppercase(); system.out.println(eng_2); //返回指定索引处的字符 char en = eng.charat(5); system.out.println(en);//e //比较字符串,返回int int num_1 = eng.compareto(eng_1); system.out.println(num_1); //返回第一个找到的子字符串的位置,若无则返回-1 string loves = "love"; int lovenum1 = eng.indexof(loves); system.out.println(lovenum1); //2 //去除字符串前后空格 string world = " i love my world "; system.out.println(world); world = world.trim(); system.out.println(world); //判断suffix是否为字符串的开始 string suffix = "world"; if (world.startswith(suffix)) system.out.println("world是字符串world的开始"); else system.out.println("world不是字符串world的开始"); //判断suffix是否为字符串的结尾 if (world.endswith(suffix)) system.out.println("world是字符串world的结尾"); else system.out.println("world不是字符串world的结尾"); //string buffer 类 //极大的提高字符串的处理速度,缺点是占内存大。在处理极大量字符时使用 stringbuffer sb = new stringbuffer("stringbuffer beautifer good"); system.out.println(sb); //添加参数到stringbuffer对象中 sb.append(","); sb.append("i love stringbuffer!"); system.out.println(sb); //删除stringbuffer对象中指定字符或字符序列 sb.deletecharat(0);//删指定位置的一个字符 system.out.println(sb); sb.delete(0, 12);//从某位置到某位置全删 system.out.println(sb); // } }
上面代码学习的朋友可以参考下
下一篇: SpringBoot多表联查(测试可用)