java字符串初识

String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实
现。 字符串是常量;它们的值在创建之后不能更改,因为 String 对象是不可变的,所以可以共享。

定义字符串

1.String s="Hello";

2.String ss=new String();// 等价于 String ss = "";

String sss = new String("Hello");// 等价于 String sss = "Hello";

了解字符串

所有类的父类Object类的equals方法(等价于==),此方法对于任何非空引用值x和y,当且仅当x和y引用同一个对象时,方法才返回true,也就是说其对比的是内存地址。
String 类的equals方法是重写了Object类的equals方法,当且仅当参数不为null,并且是与此对象表示相同字符列的String对象时,才返回true,也就说对比的是字符串的内容。

系统专门为String 提供了常量池。 例如 String s1=“hello”;String s2=“hello”; 当声明 s1时会在常量池中产生一个内容为“hello”的常量,且让S1指向它,当声明第二个内容一样的字符串时,不会再新增一个字符串,此时会直接将s2也指向“hello”这个字符串。所以s1.equals(s2) 为true,s1==s2。
最后,例如 String s3= new String(“hello”); String s3= new String(“hello”);在堆中,只要new出来的都是新对象 ,S1.equals(s3) 为true,s1!=s3。

常用方法

  • char charAt(int index) :返回指定索引处的 char 值。
  • int indexOf(int ch) :返回指定字符在此字符串中第一次出现处的索引。
  • int indexOf(int ch, int fromIndex) :返回在此字符串中第一次出现指定字符处的索引,从指定的
  • 索引开始搜索。
  • int indexOf(String str) :返回指定子字符串在此字符串中第一次出现处的索引。
  • int indexOf(String str, int fromIndex) :返回指定子字符串在此字符串中第一次出现处的索引,
  • 从指定的索引开始。
  • int lastIndexOf(int ch) :返回指定字符在此字符串中最后一次出现处的索引。
  • int length() :返回此字符串的长度。
  • String[] split(String regex) : 根据给定正则表达式的匹配拆分此字符串。
  • String substring(int beginIndex) :返回一个新的字符串,它是此字符串的一个子字符串。
  • String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串的一个子符串。
  • String replace(char oldChar, char newChar) :返回一个新的字符串,它是通过用 newChar 替
  • 换此字符串中出现的所有 oldChar 得到的。
  • String replace(CharSequence target, CharSequence replacement) :使用指定的字面值替换序列
  • 替换此字符串所有匹配字面值目标序列的子字符串。
  • String toLowerCase() :使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
  • String toUpperCase() :使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
  • String trim() :返回字符串的副本,忽略前导空白和尾部空白

注意:字符串不要使用等于符号判断是否相等

 在java中 == 判断的值的,不能用来判断字符串

因为字符串的创建方式有多种,不同的创建方式,地址(指针)可能不一样
基本数据类型都是可以使用 == 判断是否相等
字符串不能用 == 判断是否相等,字符串中 == 判断的字符串的地址
equals判断是字符串的值;

 例1:根据完整的路径从路径中分离文件路径、文件名及扩展名传递一个路径 c://a//b//c.avi,返回该文件的后缀名(lastIndexOf(int ch),substring(int beginIndex))

public static void main(String[] args) {
		System.out.println("请输入路径");
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		method(s);
			
}
public static void method(String s){
	int a=s.lastIndexOf(".");
	s=s.substring(a);
	System.out.println(s);
}

例2:输入一个字符串,判断该字符串是否是回文字符串(charAt())

public static void main(String[] args) {
			System.out.println("请输入一个字符串");
			Scanner sc = new Scanner(System.in);
			String s = sc.next();
			boolean b=true;
			int a=0;
			int b1=s.length()-1;
			for (int i = 0; i <= s.length() / 2; i++) {
			if ((s.charAt(a))==(s.charAt(b1))) {
					b=true;
				}else {
					b=false;
				}
			}
			if (b) {
				System.out.println("是回文字符串");
			}else{
				System.out.println("不是回文字符串");
			}
}

例3:去掉字符串中的所有空格(replace(char oldChar, char newChar))

public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	System.out.println("请输入字符串");
	String s=sc.nextLine();
	method(s);
}
static void method(String s){
	s=s.replace(" ", "");
	System.out.println(s);
}
}

例4:将字母全部转换为大写或小写(toLowerCase(),toUpperCase())

public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	System.out.println("请输入字符串");
	String s=sc.nextLine();
	method(s);
}
static void method(String s){
	s=s.toUpperCase(); //s=s.toLowerCase();
	System.out.println(s);
}
}

例5:接收用户输入的一句英文,将其中的单词以反序输出(split(String regex))

public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	System.out.println("请输入字符串");
	String s=sc.nextLine();
	method(s);
}
static void method(String s){
	String[] s1=s.split(" ");
	int a=0;
	int b=s1.length-1;
	for(int i=0;i<=s1.length/2;i++) {
		String temp=s1[i];
		s1[a]=s1[b];
		s1[b]=temp;
		a++;
		b--;
	}
	for(int i=0;i<=s1.length;i++) {
		System.out.print(s1[i]+" ");
	}
}

例6:从请求地址中提取出用户名和域名(indexOf(String str),lastIndexOf(int ch))

public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	System.out.println("请输入地址");
	String s=sc.nextLine();
	method(s);
}
static void method(String s) {
    String userName;
    userName = s.substring(s.indexOf("userName")+9, s.lastIndexOf("pwd")-1); // 左闭右开
    String urlName;
    urlName = s.substring(s.indexOf("//")+2, s.indexOf("?"));
    System.out.println("用户名:"+userName+"和域名:"+urlName);
}

例7:让用户输入一句话,找出所有"呵"的位置。(charAt(int index))

public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	System.out.println("请输入字符串");
	String s=sc.nextLine();
	method(s);
}
static void method(String s) {
	for(int i=0;i<s.length();i++) {
		if(s.charAt(i)=='呵')
		System.out.print(i+" ");
	}
}

例8:让用户输入一句话,判断这句话中有没有邪恶,如果有邪恶就替换成这种形式然后输出,如:“老牛很邪恶”,输出后变成”老牛很**”(contains(CharSequence s),replace(char oldChar, char newChar))

public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	System.out.println("请输入字符串");
	String s=sc.nextLine();
	method(s);
}
static void method(String s){
	if(s.contains("邪恶")) {
		s=s.replace("邪恶", "**");
	}
	System.out.println(s);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值