第二部分面向对像基础第五章Strng类中方法的使用

package com.java.oop.day2;



import java.util.Calendar;

import java.util.Formatter;

import java.util.Locale;



public class StringClass {



	public static void main(String[] args) {

		// TODO Auto-generated method stub

		stringtoCharArray();//字符与字符串转换

		strinLenght();//字符串长度

		stringIndexOf();//字符串查找

		stringSubstring();//字符串截取

		strngSplit();//字符串拆分

		stringEquals();//字符串比较

	}

	

	//将此字符串转换为一个新的字符数组。 

	public static void stringtoCharArray(){

		String str = new String("hello");

		char c[] = str.toCharArray();

		for(int i = 0 ; i < str.length();i++){

			System.out.print(c[i]+" ");

		}

		System.out.println();

		String str1 = new String(c);//将字符数组转换为一个新的字符串。 

		System.out.println(str1); 

		System.out.println(str.charAt(3));//取出字符串中的第四个字符。

	}

	//字符串长度

	public static void strinLenght(){

		String str = "OF";

		System.out.println("字符串长度"+str.length());

	}

	//查找字符串是否存在

	public static void stringIndexOf(){

		String str = "  string  ";

		System.out.println(str.indexOf("g"));

		System.out.println(str.indexOf("t",1));

		//去除字符串两边的空格

		System.out.println(str.trim());

		//System.out.println(str);

	}

	//字符串截取

	public static void stringSubstring(){

		String str = "hello";

		String str1 = str.substring(0, 3);

		System.out.println("字符串截取="+str1);

	}

	//拆分字符串

	public static void strngSplit(){

		String str = "he,ll,o";

		String s[] = str.split(",");

		for(int i = 0; i <s.length ;i++){

			System.out.println(s[i]);

		}

	}

	//字符串比较

	public static void stringEquals(){

		String str = "hello";

		String str1 = "hello";

		String str2 = "";

		if(str.equals(str1))System.out.println(true);

		if(str2.isEmpty())System.out.println(true);

		//格式化日期

		StringBuilder sb = new StringBuilder();

		Formatter formatter = new Formatter(sb, Locale.US);

		formatter.format("年-月-日:: %1$tY-%1$te-%1$tm", Calendar.getInstance());

		System.out.println();

		System.out.println(formatter);

		System.out.println();

		System.out.format("年-月-日: %1$tY-%1$te-%1$tm", Calendar.getInstance());

		System.out.println();

		Formatter formatter1 = new Formatter(sb, Locale.US);

		formatter1.format("时间: %1$tH:%1$tM", Calendar.getInstance());

		System.out.println();

		System.out.println(formatter1);

		

	}

}


你可能感兴趣的:(基础)