初次看键java的对字符串的操作,和C语言提供的字符串操作函数比较起来瞬间感觉C语言有点复杂,java功能似乎更强大,操作简便,实现功能也强。
import java.util.Date;
public class HelloJava {
static String s1=" HELLO,I come here!\n"; //s1=new String("hello");
static int i=0;
public static void main(String[] args)
{
System.out.println(s1); //打印字符串
System.out.println(s1.length());//字符串长度
System.out.println(s1.indexOf('H'));//获取字符的位置
System.out.println(s1.indexOf("come"));//获取子字符串位置的位置
System.out.println(s1.lastIndexOf("ere"));//字符最后一次出现的位置
System.out.println(s1.charAt(5));//获取索引为5的位置的字符
System.out.println(s1.substring(9));//获取索引9开始向后的字符串
System.out.println(s1.substring(0, 2));//指定前后索引获取子字符串
System.out.println(s1.trim());//去掉尾部和前部的空格
System.out.println(s1.replace('E', 'e'));//字符串变量的替换
System.out.println(s1.startsWith("HE"));//判断字符串前缀字符
System.out.println(s1.endsWith("here!\n"));//判断字符串结尾的后缀
System.out.println(s1.equals(" HELLO,I come here!\n"));//比较字符串是否相等
System.out.println(s1.equalsIgnoreCase(" hello,I come here!\n"));//比较字符串无视大小
System.out.println(s1.compareTo(" HELLO,I come here!\n"));//比较字符串是否相等
System.out.println(s1.toLowerCase());//小写转换
System.out.println(s1.toUpperCase());//大写转换
String[] s2=s1.split(","); //分割字符串
for(i=0;i<s2.length;i++)
{
System.out.println(s2[i]);
}
Date date=new Date();
System.out.println(s1.format("%tY",date));//格式转换
System.out.println(s1.format("%tB",date));
System.out.println(s1.format("%td",date));
}
}