关闭

一些总结

标签: stringcalendarclassimportmethodsn2
1000人阅读 评论(0) 收藏 举报

字符串逆转:

public class getStr{
public String aa(String s)
{
StringBuffer sb=new StringBuffer();
for(int i=s.length()-1; i>=0;i--)
{
  sb.append(s.charAt(i));
}
System.out.println("sb is :"+sb);
return sb.toString() ;
}
public static void main(String args[]){
 String s="1234567890";
    getStr b=new getStr();
    b.aa(s);
}  
}

排序:

import java.util.Arrays;
class ArraySort {
public static void main(String[] args) {
char [] a = {'a','f','h','k','d','r','h','b','v','j','g','p'};
Arrays.sort(a);
for (int i = 0;i < a.length ;i++){
System.out.println(a[i]);
}
}
}

 

字符串替换:

public class split{
 
 public static void main(String args[]){
  
  String str="00,11,22,33,44,55,66,77,88";
  String[] aa=str.split(",",3);
  System.out.println("aa.length is :"+aa.length);
  for (int i=0;i<aa.length;i++){
  System.out.println("aa[i] is :"+aa[i]);
  }
 }
}

StringTokenizer的用法:

import  java.util.*; 
public  class  UseStringTokenizer 

public  static  void  main(String  args[]) 
   { 
 String  s1="I  am  Geng.X.y,she  is  my               girlfriend"; 
 String  s2="Lowood?what  is  that?"; 
 StringTokenizer  fenxi_1=new  StringTokenizer(s1,"  ,");//空格和逗号做分隔符。 
 StringTokenizer  fenxi_2=new  StringTokenizer(s2,"  ?");//问号和空格做分隔符。 
 int  n1=fenxi_1.countTokens(),n2=fenxi_2.countTokens(); 
while(fenxi_1.hasMoreTokens()) 
   {  String  s=fenxi_1.nextToken(); 
       System.out.println(s); 
   } 
      
       System.out.println("s1有单词:"+n1+"个"); 
     while(fenxi_2.hasMoreTokens()) 
         {  String  s=fenxi_2.nextToken(); 
             System.out.println(s); 
         } 
   System.out.println("s2有单词:"+n2+"个"); 
   }   


日期计算:

import java.util.*;
public class Year{

public static void main(String args[]){
Calendar calendar=Calendar.getInstance();
int year=calendar.get(Calendar.YEAR);
System.out.println("year is :"+year);
int month=calendar.get(Calendar.MONTH);
System.out.println("month is :"+month);

int day=calendar.get(Calendar.DAY_OF_MONTH);
int days=calendar.get(Calendar.DATE);
System.out.println("day  is :"+day);
System.out.println("days is :"+days);

int hour=calendar.get(Calendar.HOUR);
System.out.println("hour is :"+hour);

int day_ofweek =calendar.get(Calendar.DAY_OF_WEEK);
System.out.println("day_ofweek is :"+day_ofweek);

System.out.println("calendar.getTime:"+calendar.getTime());

System.out.println("calendar.getTimeInMillis:"+calendar.getTimeInMillis());
}
 
}

 

//two methods about replace characters;


public class Myreplaceall{
 
 
 public static void main(String args[]){
  
  String str="abc.ef.gh";
  //String str_1=str.replaceAll("//.","_");
  String str_1=str.replaceAll("[.]","_");
  System.out.println("str_1 is :"+str_1);
 
  String str_2="ab*cd*e*f*g";
 // if(str_2.lastIndexOf("*") > -1) {
        str_2 = str_2.replace("*", ",");
        // }
        
        System.out.println("str_2 is :"+str_2);

 }
}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:48804次
    • 积分:656
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:6篇
    • 译文:0篇
    • 评论:13条
    最新评论