java大量处理字符串_Java常用字符串方法小结

本文详细介绍了Java中字符串的各种操作方法,包括字符串的声明与赋值、长度获取、连接、比较、截取、替换等基本操作,并展示了如何进行大小写转换、字符查找、去除空格等进阶操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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);

//

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值