public static void main(String[] args) {
char_test a=new char_test();
String result[]=new String[10];
String temp="";
String s="iPhone 6 的相机镜头维持在 800 万画素,iSight添加了Focus Pixels ,提升为1.5 微米大像素与/2.2 大光圈,让自动对焦更快。新的 iPhone 6镜头还采用面孔识别功能、曝光控制以及更强大的全景模式。"
+ "此外,iPhone 6 还沿用数位防手震功能;"
+ "iPhone 6 Plus 则加入了 OIS 光学防手震功能。";
int j=0;
for(int i=0;i{
if(a.isChar(s.charAt(i)) && a.isChar(s.charAt(i+1)) )//判断是否为英文
{
temp+=s.charAt(i)+"";
}
else if(a.isChar(s.charAt(i))==true&& a.isChar(s.charAt(i+1))==false)//判断英文结束
{
temp+=s.charAt(i)+"";
result[j]=temp;
j++;
temp="";
}
else if(j>=10)//截取前十个字
break;
else
{
result[j]=s.charAt(i)+"";
j++;
}
}
for(int i=0;i<10;i++)//输出前十个字
System.out.print(result[i]);
}
boolean isChar(char s)
{
boolean b=false;
if(s<='z' && s>='a' ||s<='Z' && s>='A' )
{
b=true;
}
return b;
}
}
String target = "iPhone 6 的相机镜头维持在 800 万画素,iSight添加了Focus Pixels 全新感光元件,提升为1.5 微米大像素与/2.2 大光圈,让自动对焦更快。新的 iPhone 6镜头还采用面孔识别功能、曝光控制以及更强大的全景模式。此外,iPhone 6 还沿用数位防手震功能;iPhone 6 Plus 则加入了 OIS 光学防手震功能。";
System.out.println(target.replaceAll("\\s", "").replaceAll("(^.{20}).+", "$1"));
public static void main(String[] args) {
String line = "iPhone 6 的相机镜头维持在 800 万画素,iSight添加了Focus Pixels 全新感光元件,提升为1.5 微米大像素与/2.2 大光圈,让自动对焦更快。新的 iPhone 6镜头还采用面孔识别功能、曝光控制以及更强大的全景模式。此外,iPhone 6 还沿用数位防手震功能;iPhone 6 Plus 则加入了 OIS 光学防手震功能。";
String regexEn = "[ ]?[a-zA-Z]+[ ]?|[ ]?([\u4e00-\u9fa5])[ ]?| ?(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*)) ?| ?[,。] ?";
Pattern pEn = Pattern.compile(regexEn);
Matcher mEn = pEn.matcher(line);
int i = 1;
StringBuffer sb = new StringBuffer();
while(mEn.find() && i<21) {
System.out.println("第"+i++ +"个字符" + mEn.group());
sb.append(mEn.group());
System.out.println();
}
System.out.println("前20字符串为:" + sb.toString());
}