📄 130.txt
字号:
第五章 字符串
例子1
class Example5_1
{ public static void main(String args[])
{ String s1,s2;
s1=new String("we are students");
s2=new String("we are students");
System.out.println(s1.equals(s2));
System.out.println(s1==s2);
String s3,s4;
s3="how are you";
s4="how are you";
System.out.println(s3.equals(s4));
System.out.println(s3==s4);
}
}
例子2
class Example5_2
{ public static void main(String args[])
{ int number=0;
String s="student;entropy;engage,english,client";
for(int k=0;k<s.length();k++)
{ if(s.regionMatches(k,"en",0,2))
{ number++;
}
}
System.out.println("number="+number);
}
}
例子3
class Example5_3
{ public static void main(String args[])
{ String a[]={"door","apple","Applet","girl","boy"};
for(int i=0;i<a.length-1;i++)
{ for(int j=i+1;j<a.length;j++)
{ if(a[j].compareTo(a[i])<0)
{ String temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(int i=0;i<a.length;i++)
{ System.out.print(" "+a[i]);
}
}
}
例子4
class Example5_4
{ public static void main(String args[])
{ String path="c:\\myfile\\2000\\result.txt";
int index=path.lastIndexOf("\\");
String fileName=path.substring(index+1);
String newName=fileName.replaceAll(".txt",".java");
System.out.println(path);
System.out.println(fileName);
System.out.println(newName);
}
}
例子5
public class Example5_5
{ public static void main(String args[])
{ double n,sum=0,item=0;
boolean computable=true;
for(int i=0;i<args.length;i++)
{ try{ item=Double.parseDouble(args[i]);
sum=sum+item;
}
catch(NumberFormatException e)
{ System.out.println("您键入了非数字字符:"+e);
computable=false;
}
}
if(computable)
{ n=sum/args.length;
System.out.println("平均数:"+n);
}
int number=123456;
String binaryString=Long.toBinaryString(number);
System.out.println(number+"的二进制表示:"+binaryString);
System.out.println(number+"的十六进制表示:"+Long.toString(number,16));
String str="1110110";
int p=0,m=0;
for(int i=str.length()-1;i>=0;i--)
{ char c=str.charAt(i);
int a=Integer.parseInt(""+c);
p=p+(int)(a*Math.pow(2,m));
m++;
}
System.out.println(str+"的十进制表示:"+p);
}
}
例子6
import java.util.Date;
import java.awt.*;
public class Example5_6
{ public static void main(String args[])
{ Date date=new Date();
Button button=new Button("确定");
System.out.println(date.toString());
System.out.println(button.toString());
}
}
例子7
import java.util.*;
public class Example5_7
{ public static void main(String args[])
{ String s="I am Geng.X.y,she is my girlfriend";
StringTokenizer fenxi=new StringTokenizer(s," ,");
int number=fenxi.countTokens();
while(fenxi.hasMoreTokens())
{ String str=fenxi.nextToken();
System.out.println(str);
System.out.println("还剩"+fenxi.countTokens()+"个单词");
}
System.out.println("s共有单词:"+number+"个");
}
}
例子8
class Example5_8
{ public static void main(String args[])
{ char c[],d[];
String s="巴西足球队击败德国足球队";
c=new char[2];
s.getChars(5,7,c,0);
System.out.println(c);
d=new char[s.length()];
s.getChars(7,12,d,0);
s.getChars(5,7,d,5);
s.getChars(0,5,d,7);
System.out.println(d);
}
}
例子9
class Example5_9
{ public static void main(String args[])
{ String s="清华大学出版社";
char a[]=s.toCharArray();
for(int i=0;i<a.length;i++)
{ a[i]=(char)(a[i]^'t');
}
String secret=new String(a);
System.out.println("密文:"+secret);
for(int i=0;i<a.length;i++)
{ a[i]=(char)(a[i]^'t');
}
String code=new String(a);
System.out.println("原文:"+code);
}
}
例子10
public class Example5_10
{ public static void main(String args[])
{ byte d[]="你我他".getBytes();
System.out.println("数组d的长度是(一个汉字占两个字节):"+d.length);
String s=new String(d,0,2);
System.out.println(s);
}
}
例子11
class Example5_11
{ public static void main(String args[ ])
{ StringBuffer str=new StringBuffer("62791720");
str.insert(0,"010-");
str.setCharAt(7 ,'8');
str.setCharAt(str.length()-1,'7');
System.out.println(str);
str.append("-446");
System.out.println(str);
str.reverse();
System.out.println(str);
}
}
例子12
public class Example5_12
{ public static void main (String args[ ])
{
String regex="\\w{1,}@\\w{1,}\56\\w{1,}" ;
String str1="zhangsan@sina.com";
String str2="li@si@dl.cn";
if(str1.matches(regex))
{ System.out.println(str1+"是一个Email地址");
}
else
{ System.out.println(str1+"不是一个Email地址");
}
if(str2.matches(regex))
{ System.out.println(str2+"是一个Email地址");
}
else
{ System.out.println(str2+"不是一个Email地址");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -