Java基本的API中,String,StringBuffer,Math,Wapper class的基本应用
import java.io.*;
public class Alone12_1 {
public static void main(String[] args) throws IOException {
String s=null;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s = br.readLine();
System.out.println(s=s.replace("ef","EFG"));
for(int i=0;i<s.length();i++)
if(i%2==0)
s=s.replace(s.substring(i),s.substring(i).toUpperCase());
else
s=s.replace(s.substring(i),s.substring(i).toLowerCase());
System.out.println(s);
for(int i=0;i<s.length();i++)
System.out.print(s.toLowerCase().charAt(s.length()-i-1));
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
import java.io.*;
public class Alone12_2 {
public static void main(String[] args) throws IOException {
StringBuffer sbuff=new StringBuffer(
new BufferedReader(
new InputStreamReader(System.in)).readLine());
for(int i=0;i<sbuff.length();i++)
if(i%2==0)
sbuff.replace(i,i+1,sbuff.substring(i,i+1).toUpperCase());
else
sbuff.replace(i,i+1,sbuff.substring(i,i+1).toLowerCase());
System.out.println(sbuff);
System.out.println(sbuff.reverse());
System.out.println(sbuff.toString().toUpperCase());
}
}
-----------------------------------------------------------------------------------------------------------------------------------
public class myString {
String str=null;
myString(String value){str=value;}
public boolean isLike(String regex)
{
if(regex.indexOf("?")!=-1)return WenHao(regex);
else if(regex.indexOf("*")!=-1)return XingHao(str,regex);
return false;
}
private boolean WenHao(String regex)
{
// ?号匹配
if(str.length()!=regex.length())return false;
for(int i=0;i<regex.length();i++)
if(str.charAt(i)!=regex.charAt(i) && regex.charAt(i)!='?')
return false;
return true;
}
private boolean XingHao(String Str,String regex)
{
// *号匹配
int Lstr=Str.length();
int Lreg=regex.length();
int x1=regex.indexOf("*");
switch(x1)
{
case -1:{
//x1=-1 regex 中没有 * 号,不需要跌归计算
if(Lstr==Lreg)
{
if(Lstr==0)return true;
for(int kk=0;kk<Lreg;kk++)//检测字符串是否匹配
if(Str.charAt(kk)!=regex.charAt(kk))return false;
return true;
}else
return false;
}
case 0:
{//x1=0 regex 中 * 号在首位
if(Lreg==1)return true;//只有一个星号,自然是匹配的,如 regex="*"
boolean right=false;
int p=0;
// *号在首位,定位 * 号 后一位
for(int k=0;k<Lstr;k++)
if(Str.charAt(k)==regex.charAt(x1+1)||regex.charAt(x1+1)=='*')
{p=k;right=true;break;}//遇到 ** 就直接 right=true;
if(right==false)return false;
else
{
if(p==Lstr)return true;
return XingHao(Str.substring(p,Lstr),regex.substring(x1+1,Lreg));
}
}
default:
{ //x1>0
for(int i=0;i<x1;i++)
if(Str.charAt(i)!=regex.charAt(i))return false;
return XingHao(Str.substring(x1,Lstr),regex.substring(x1,Lreg));
}
}
}
public static void main(String[] args) {
System.out.println("str=ABCD regex=ABC? :"+new myString("ABCD").isLike("ABC?"));
System.out.println("str=ABCD regex=A??? :"+new myString("ABCD").isLike("A???"));
System.out.println("str=ABCD regex=A?? :"+new myString("ABCD").isLike("A??"));
System.out.println("str=ABCD regex=?BC? :"+new myString("ABCD").isLike("?BC?"));
System.out.println("str=ABCD regex=*B*D :"+new myString("ABCD").isLike("*B*D"));
System.out.println("str=ABCD regex=*BCD :"+new myString("ABCD").isLike("*BCD"));
System.out.println("str=ABCD regex=*A*B*D :"+new myString("ABcCD").isLike("*A*B*D"));
}
}