第14 章 : 类库使用案例分析
59 StringBuffer使用
使用StringBuffer追加26个小写字母。逆序输出,并删除前5个字符 StringBuffer允许修改 String不允许修改
StringBuffer buff = new StringBuffer();
for(int i = 'a'; i
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Demo {
public static void main(String[] args) {
String html = "";
String regex = "\\w+=\"[a-zA-Z0-9,\\+]+\"";
Matcher matcher = Pattern.compile(regex).matcher(html);
while (matcher.find()){
String temp = matcher.group(0);
String[] result = temp.split("=");
System.out.println(result[0] + "\t" + result[1].replaceAll("\"", ""));
/**
* face Arial,Serif
* size +2
* color red
*/
}
}
}
65 国家代码
实现国际化应用 输入国家代号,调用资源文件 3个资源文件
# message.properties
info=默认资源
# message_en_US.properties
info=英文资源
# message_zh_CN.properties
info=中文资源
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;
class MessageUtil {
// 将固定的内容定义为常量
private static final String CHINA = "cn";
private static final String ENGLISH = "en";
private static final String BASENAME = "message";
private static final String KEY = "info";
public static String getMessage(String country) throws UnsupportedEncodingException {
Locale locale = getLocale(country);
if (locale == null) {
return null;
} else {
ResourceBundle bundle = ResourceBundle.getBundle(BASENAME, locale);
return new String(bundle.getString(KEY).getBytes("ISO-8859-1"), "utf-8");
}
}
private static Locale getLocale(String country) {
switch (country) {
case CHINA:
return new Locale("zh", "CN");
case ENGLISH:
return new Locale("en", "US");
default:
return null;
}
}
}
class Demo {
public static void main(String[] args) throws UnsupportedEncodingException {
if (args.length other.score){
return 1;
} else if (this.score
关注
打赏