前几天经过学习,认识了java中的API文档。API在java编程里就像一部字典同样,经过查询API文档咱们能够清楚地查看java自带类中的经常使用方法都是什么。就像小的时候学习如何使用汉语字典同样,这是一个工具文档。有了它就省去了机械的去记忆这些类中的方法,有需求的时候直接经过查询就行,就像咱们根本不须要记住全部汉字,只要记住如何查询字典就行。
下面就简短的列举几个关于String类中一些方法,主要仍是学习他们是怎么运行的,以及调用他们会有什么样的结果:
① String类中的equals函数:equals的做用是比较两个字符串里面内容是不是相同的,具体用法以下:
public class Test{
public static void main(String args[])
{
String a = "String";
String b = "string";
System.out.print(a.equals(b));
}
}
注:运行结果是在控制台里输出FALSE;equals函数的返回值是Boolean型。
② String类中的endwith函数:endsWith的做用是查看某个字符串是否是按照另外一个字符串的顺序结尾的,具体用法以下:
public class Test{
public static void main(String args[])
{
String a = "String";
String b = "ing";
System.out.print(a.endsWith(b));
}
}
注:运行结果是在控制台里输出TRUE;endsWith函数的返回值是Boolean型。
③ String类中的toCharArray函数:toCharArray的做用是把一个字符串按照单个字符串拆分为char型数组,具体用法以下:
public class Test{
public static void main(String args[])
{
String a = "String";
char[] b = new char[6];
b = a.toCharArray();
for(int i = 0 ; i < b.length ; i++)
{
System.out.println(b[i]);
}
}
}
注:运行结果是在控制台里输出为:
S
t
r
i
n
g
④ String类中的toLowerCase函数:toLowerCase的做用是将字符串内全部的大写字母转换成小写字母,具体用法以下:
public class Test{
public static void main(String args[])
{
String a = "StRinG123";
System.out.print(a.toLowerCase());
}
}
注:运行结果为:string123;只是单纯的转换字母,对于数字等非字母字符不作处理。
⑤ String类中的toUpperCase函数:toUpperCase的做用是将字符串内全部的小写字母转换成大写字母,具体用法以下:
public class Test{
public static void main(String args[])
{
String a = "StRinG123";
System.out.print(a.toUpperCase());
}
}
注:运行结果为:STRING123;只是单纯的转换字母,对于数字等非字母字符不作处理。
⑥ String类中的concat函数:concat的做用是将指定字符串链接到此字符串的结尾,具体用法以下:
public class Test{
public static void main(String args[])
{
String a = "Str";
String b = "ing";
System.out.print(a.concat(b));
}
}
注:运行结果为:String;若是参数字符串的长度为
0
,则返回此
String
对象。不然,建立一个新的
String
对象。