(1)今天做了什么; (2)明天准备做什么? (3)遇到的问题,如何解决?

今天学习了菜鸟教程实例部分

一、字符串

1.字符串比较——compareTo方法

public class Main
{
	public static void main(String[] args)
	{
		String str = "Hello World";
		String anotherString = "hello world";
		Object objStr = str;
		System.out.println(str.compareTo(anotherString));
		System.out.println(str.compareToIgnoreCase(anotherString));//忽略大小写
		System.out.println(str.compareTo(objStr.toString()));
	}
}

2.字符串最后出现的位置——lastIndexOf方法

//字符串最后一次出现的位置
public class Main
{
	public static void main(String[] args)
	{
		String strOrig = "Hello world,Hello Runoob";
		int lastIndex = strOrig.lastIndexOf("Runoob");
		if(lastIndex == -1)
		{
			System.out.println("没有找到字符串 Runoob");
		}
		else
		{
			System.out.println("Runoob 字符串最后出现的位置: " + lastIndex);
		}
	}
}

3.substring函数的使用,以及删除字符串中的一个字符

//删除字符串中的一个字符
public class Main
{
	public static void main(String[] args)
	{
		String str = "this is java";
		str = removeCharAt(str,4);
		System.out.println(str);
	}
	public static String removeCharAt(String string,int num)
	{
		//substring(x,y) :截取从下标为x的字符开始,到下标y-1的字符结束 substring(x):截取从下标为x的字符到字符串末尾的字符串
		string = string.substring(0, num) + string.substring(num + 1);
		return string;
	}
}

4.字符串替换——replace方法

public class Main
{
	public static void main(String[] args)
	{
		String str = "Hello World";
		System.out.println(str.replace('H','W'));
		System.out.println(str.replace("He", "Wa"));
		System.out.println(str.replace("He", "Ha"));
	}
}

 明天继续字符串实例。

今天没有遇到什么问题。

相关文章:

  • 2022-01-19
  • 2022-01-26
  • 2021-09-05
  • 2021-10-06
  • 2021-07-03
  • 2021-11-04
  • 2022-12-23
猜你喜欢
  • 2022-02-08
  • 2021-12-01
  • 2021-09-26
  • 2021-12-03
  • 2021-09-27
  • 2021-09-07
  • 2021-05-24
相关资源
相似解决方案