(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"));
}
}
明天继续字符串实例。
今天没有遇到什么问题。