java string举例说明3

来源:互联网 发布:域名怎么看 编辑:程序博客网 时间:2024/06/05 22:35

例如27:

package Chapter06.string;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringDemo_27 {
 private static String REG_EXP = "^([0-9]{3}-?[0-9]{8})|([0-9]{4}-?[0-9]{7})$";
 /** 使用String类中的matches方法利用正则表达式匹配的方法 */
 public static boolean useMatches(String phoneNum) {
  if (phoneNum != null) {
   return phoneNum.matches(REG_EXP);
  } else {
   return false;
  }
 }
 /** 使用Pattern的compile方法和Matcher的matcher方法共同匹配 */
 public static boolean usePattern(String phoneNum) {
  Pattern p = Pattern.compile(REG_EXP);
  // 创建一个Matcher,并进行精确匹配
  Matcher m = p.matcher(phoneNum);
  return m.matches();
 }

例如26:

package Chapter06.string;

import java.security.MessageDigest;

public class StringDemo_26 {
 private final static String[] hexArray = { "0", "1", "2", "3", "4", "5",
   "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; // 存储十六进制值的数组
 /** 根据指定的字符串,创建加密后的字符串 */
 public static String createEncrypPassword(String string) {
  return encrypByMD5(string);
 }
 /** 检验输入的密码是否正确 */
 public static boolean verificationPassword(String password, String string) {
  if (password.equals(encrypByMD5(string))) {
   return true;
  } else {
   return false;
  }
 }
 /** 对指定的字符串进行MD5加密 */
 private static String encrypByMD5(String originString) {
  if (originString != null) {
   try {
    // 创建具有MD5算法的信息摘要
    MessageDigest md = MessageDigest.getInstance("MD5");
    // 使用指定的字节数组对摘要进行最后更新,然后完成摘要计算
    byte[] results = md.digest(originString.getBytes());
    // 将得到的字节数组变成字符串返回
    String resultString = byteArrayToHex(results);
    return resultString.toUpperCase();
   } catch (Exception ex) {
    ex.printStackTrace();
   }
  }
  return null;
 }
 /** 将字节数组转换成16进制,并以字符串的形式返回 */
 private static String byteArrayToHex(byte[] b) {
  StringBuffer resultSb = new StringBuffer();
  for (int i = 0; i < b.length; i++) {
   resultSb.append(byteToHex(b[i]));
  }
  return resultSb.toString();
 }
 /** 将一个字节转换成16进制,并以字符串的形式返回 */
 private static String byteToHex(byte b) {
  int n = b;
  if (n < 0)
   n = 256 + n;
  int d1 = n / 16;
  int d2 = n % 16;
  return hexArray[d1] + hexArray[d2];
 }
 public static void main(String[] args) {
  String password = StringDemo_26.createEncrypPassword("hahaxiao1984");
  System.out.println("对password=hahaxiao1984使用MD5算法加密后的字符串如下:\n  "
    + password);
  String string = "hahaxiao1999";
  System.out.println("hahaxiao1999是正确的密码吗?"
    + StringDemo_26.verificationPassword(password, string));
  string = "hahaxiao1984";
  System.out.println("hahaxiao1984是正确的密码吗?"
    + StringDemo_26.verificationPassword(password, string));
 }
}

例如25:

package Chapter06.string;
public class StringDemo_25 {
 public static final int LEFT = 0;   // 左对齐格式
 public static final int CENTER = 1;  // 居中格式
 public static final int RIGHT = 2;  // 右对齐格式
 private int format;     // 当前对齐格式
 private int maxLength;    // 一行的最大长度
 /** 默认构造函数 */
 public StringDemo_25() {
  // 默认为居中对齐,一行的最大长度为80
  this.format = CENTER;
  this.maxLength = 80;
 }
 /** 构造一个字符串对齐器,需要传入一行的根据最大长度和对齐的格式。 */
 public StringDemo_25(int maxLength, int format) {
  this();  // 首先构造一个默认字符串对齐器
    // 根据传入参数修改字符串对齐器的属性
  this.setFormat(format);
  this.setMaxLength(maxLength);
 }
 /** 将字符串按指定的对齐格式进行齐 */
 public String format(String s) {
  StringBuffer where = new StringBuffer();
  // 获得一个新长度为行最大长度和s长度的较小值
  int wLength = Math.min(s.length(), this.maxLength);
  String w = s.substring(0, wLength); // 根据新长度从待对齐的字符串s中获取一个子串
  switch (this.format) {    // 根据对齐模式,选择空格的合适的位置
  case RIGHT:
   // 如果是右对齐,则空格放在左边
   addSpace(where, maxLength - wLength);
   // 将字符串添加在右边
   where.append(w);
   break;
  case CENTER:
   // 居中对齐,将空格字符平均分在字符串两边。
   int start = where.length();
   addSpace(where, (maxLength - wLength) / 2);
   where.append(w);
   addSpace(where, (maxLength - wLength) / 2);
   // 调整舍入误差
   addSpace(where, maxLength - (where.length() - start));
   break;
  case LEFT:
   // 右对齐,将空格字符放在字符串右边。
   where.append(w);
   addSpace(where, maxLength - wLength);
   break;
  }
   // 如果原字符串的长度大于一行的最大长度,则将余下部分放入下一行
  if (s.length() > wLength) {
   String remainStr = s.substring(wLength);
   where.append("\n" + this.format(remainStr));
  }
  return where.toString();
 }
 /** 追加空格字符。 */
 protected final void addSpace(StringBuffer to, int howMany) {
  for (int i = 0; i < howMany; i++)
   to.append(" ");
 }
 public int getFormat() {
  return format;
 }
 /** 设置对齐的格式 */
 public void setFormat(int format) {
  switch (format) {
  case LEFT:
  case CENTER:
  case RIGHT:
   this.format = format;
   break;
  default:
   System.out.println("无效的对齐格式");
  }
 }
 public int getMaxLength() {
  return maxLength;
 }
 /** 设置字符串一行中最多可以显示的字符数 */
 public void setMaxLength(int maxLength) {
  if (maxLength < 0) {
   System.out.println("最大长度的值必须大于0.");
  } else {
   this.maxLength = maxLength;
  }
 }
 public static void main(String[] args) {
  System.out.println("字符的居中对齐方式如下:");
  StringDemo_25 formatter = new StringDemo_25(15, StringDemo_25.CENTER);// 一行最多15个字符,居中显示
  System.out.println(formatter.format("123456"));
  System.out.println(formatter.format(Integer.toString(56)));
  System.out.println(formatter.format("abcdefghijklmnopqrstuvwxyz"));
  System.out.println("\n字符的左对齐方式如下:");
  formatter = new StringDemo_25(15, StringDemo_25.LEFT); // 一行最多15个字符,左对齐显示
  System.out.println(formatter.format("&%54$$"));
  System.out.println(formatter.format(Integer.toString(83)));
  System.out.println(formatter.format("abcdefghijklmnopqrstuvwxyz"));
  System.out.println("\n字符的右对齐方式如下:");   // 一行最多15个字符,右对齐显示
  formatter = new StringDemo_25(15, StringDemo_25.RIGHT);
  System.out.println(formatter.format("123456"));
  System.out.println(formatter.format(Integer.toString(8)));
  System.out.println(formatter.format("abcdefghijklmnopqrstuvwxyz"));
 }
}
例如24:

package Chapter06.string;
public class StringDemo_24 {
 public static final int NULL_MARK = 0;    // 标记为空或者结束符
 public static final int SEPARATION_MARK = 1;  // 标记为分隔符
 public static final int VAR_MARK = 2;    // 标记为变量
 public static final int NUM_MARK = 3;    // 标记为数字
 public static final int GRAMMAR_Exception = 0;  // 语法错误
 public static final int UNEND_Exception = 1;   // 括号没有结束错误
 public static final int NULLEXP_Exception = 2;   // 表达式为空错误
 public static final int BYZERO_Exception = 3;   // 被0除错误
 public static final String[] MESSAGES_Exception = { "语法错误", "大括号没有成对错误",
   "表达式为空错误", "被0除错误" };
 public static final String END = "\0";    // 表达式的结束标记
 private String expre;      // 表达式字符串
 private int position;       // 求值器当前指针在表达式中的位置
 private String current;      // 求值器当前处理的标记
 private int currentType;      // 求值器当前处理标记的类型
 private double varArray[] = new double[26];  // 变量的数组
 /** 求值一个表达式,返回表达式的值。 */
 public double analysis(String str) throws Exception {
  double result;
  this.expre = str;
  this.position = 0;
  // 获取第一个标记
  this.getMark();
  if (this.current.equals(END)) {
  // 没有表达式异常
   this.proce_Exception(NULLEXP_Exception);
  }
  result = this.proce_Evalua();    // 处理赋值语句
  // 处理完赋值语句,应该就是表达式结束符,如果不是,则返回异常
  if (!this.current.equals(END)) {
   this.proce_Exception(GRAMMAR_Exception);
  }
  return result;
 }
 private double proce_Evalua() throws Exception {
  double result;      // 结果
  int var_Index;     // 变量下标
  String oldMark;     // 旧标记
  int oldMarkType;     // 旧标记的类型
   // 如果标记类型是变量
  if (this.currentType == VAR_MARK) {
   // 保存当前标记
   oldMark = new String(this.current);
   oldMarkType = this.currentType;
   // 取得变量的索引,本求值器只支持一个字目的变量,
   // 如果用户的变量字母长度大于1,则取第一个字母当作变量
   var_Index = Character.toUpperCase(this.current.charAt(0)) - 'A';
   // 获得下一个标记
   this.getMark();
   // 如果当前标记不是等号=
   if (!this.current.equals("=")) { // 判断当前表达式是否是赋值运算
    this.rollBack();    // 回滚
    // 不是一个赋值语句,将标记恢复到上一个标记
    this.current = new String(oldMark);
    this.currentType = oldMarkType;
   } else {
    // 如果当前标记是等号=,即给变量赋值,形式如a = 3 + 5;
    // 则计算等号后面表达式的值,然后将得到的值赋给变量
    this.getMark();
    // 因为加减法的优先级最低,所以计算加减法表达式。
    result = this.arith_AddExpre();
    // 将表达式的值赋给变量,并存在实例变量vars中。
    this.varArray[var_Index] = result;
    return result;
   }
  }
  return this.arith_AddExpre(); // 调用arith_AddExpre方法,进行加减法计算表达式的值。
 }
 private double arith_AddExpre() throws Exception {
  char op;      // 操作符
  double result;     // 结果
  double partialResult;   // 当前子表达式的结果
  result = this.arith_MulExpre();  // 调用arith_MulExpre方法获取当前子表达式的值
    // 当前标记的第一个字母是加减号,则继续进行加减法运算。
  while ((op = this.current.charAt(0)) == '+' || op == '-') {
   this.getMark(); // 取下一个标记
   partialResult = this.arith_MulExpre(); // 调用arith_MulExpre方法获取当前子表达式的值
   switch (op) {
   case '-':
    // 如果是减法,则用已处理的子表达式的值减去当前子表达式的值
    result = result - partialResult;
    break;
   case '+':
    // 如果是加法,用已处理的子表达式的值加上当前子表达式的值
    result = result + partialResult;
    break;
   }
  }
  return result;
 }
 private double arith_MulExpre() throws Exception {
  char op;      // 运算符
  double result;     // 表达式结果
  double currentResult;   // 子表达式的结果
  // 用指数运算计算当前子表达式的值
  result = this.indexExpre();
  // 如果当前标记的第一个字母是乘、除或者取模运算符,则继续进行乘除法运算。
  while ((op = this.current.charAt(0)) == '*' || op == '/' || op == '%') {
   this.getMark();    // 取下一个标记
  // 用指数运算计算当前子表达式的值
   currentResult = this.indexExpre();
   switch (op) {
   case '*':
    // 如果是乘法,则用已处理子表达式的值乘以当前子表达式的值
    result = result * currentResult;
    break;
   case '/':
    // 如果是除法,判断当前子表达式的值是否为0,如果为0,则抛出被0除异常
    // 除数不能为0
    if (currentResult == 0.0) {
     this.proce_Exception(BYZERO_Exception);
    }
    // 除数不为0,则进行除法运算
    result = result / currentResult;
    break;
   case '%':
    // 如果是取模运算,也要判断当前子表达式的值是否为0
    // 如果为0,则抛出被0除异常
    if (currentResult == 0.0) {
     this.proce_Exception(BYZERO_Exception);
    }
    // 进行取模运算
    result = result % currentResult;
    break;
   }
  }
  return result;
 }
 private double unaryOperator() throws Exception {
  double result;  // 表达式结果
  String op;  // 运算符
  op = "";
  // 如果当前标记类型为分隔符,而且分隔符的值等于+或者-。
  if ((this.currentType == SEPARATION_MARK) && this.current.equals("+")
    || this.current.equals("-")) {
   op = this.current;
   this.getMark();
  }
  // 用括号运算计算当前子表达式的值
  result = this.parenthesis();
  if (op.equals("-")) {
  // 如果操作符为-,则表示负数,将子表达式的值变为负数
   result = -result;
  }
  return result;
 }
 private double parenthesis() throws Exception {
  double result;      // 表达式结果
  if (this.current.equals("(")) {  // 如果当前标记为左括号,则表示是一个括号运算
   this.getMark();     // 取下一个标记
   result = this.arith_AddExpre(); // 调用arith_AddExpre方法
   // 判断括圆括号是否匹配,如果当前标记不等于右括号,抛出括号不匹配异常
   if (!this.current.equals(")")) {
    this.proce_Exception(UNEND_Exception);
   }
   this.getMark();     // 否则取下一个标记
  } else {
   // 如果标记不是左括号,表示不是一个括号运算,则调用varNumber方法
   result = this.varNumber();
  }
  return result;
 }
 private double varNumber() throws Exception {
  double result = 0.0;  // 结果
  switch (this.currentType) {
  case NUM_MARK:
   // 如果当前标记类型为数字
   try {
   // 将数字的字符串转换成数字值
    result = Double.parseDouble(this.current);
   } catch (NumberFormatException exc) {
    this.proce_Exception(GRAMMAR_Exception);
   }
   this.getMark();  // 取下一个标记
   break;
  case VAR_MARK:
   // 如果当前标记类型是变量,则取变量的值
   result = this.seekVar(current);
   this.getMark();
   break;
  default:
   this.proce_Exception(GRAMMAR_Exception);
   break;
  }
  return result;
 }
 private double seekVar(String vname) throws Exception {
  // 判断是否有语法异常发生,如果变量的第一个字符不是字母,则抛出语法异常
  if (!Character.isLetter(vname.charAt(0))) {
   proce_Exception(GRAMMAR_Exception);
   return 0.0;
  }
  // 从实例变量数组varArray中取出该变量的值
  return varArray[Character.toUpperCase(vname.charAt(0)) - 'A'];
 }
 private double indexExpre() throws Exception {
  double result;     // 表达式结果
  double currentResult;   // 子表达式的值
  double ex;     // 指数的底数
  int t;      // 指数的幂
  result = this.unaryOperator(); // 调用unaryOperator方法,获取当前表达式中的底数值
  if (this.current.equals("^")) { // 如果当前标记为"^"运算符,则为指数计算
   // 获取下一个标记,即获取指数的幂
   this.getMark();
   currentResult = this.indexExpre();
   ex = result;
   if (currentResult == 0.0) {
   // 如果指数的幂为0,则指数的值为1
    result = 1.0;
   } else {
    // 否则,指数的值为个数为指数幂的底数相乘的结果。
    for (t = (int) currentResult - 1; t > 0; t--) {
     result = result * ex;
    }
   }
  }
  return result;
 }
 private void rollBack() {
  if (this.current == END) {
   return;
  }
  // 求值器当前指针往前移动
  for (int i = 0; i < this.current.length(); i++) {
   this.position--;
  }
 }
 private void proce_Exception(int errorType) throws Exception {
  // 遇到异常情况时,根据错误类型,取得异常提示信息,将提示信息封装在异常中抛出
  throw new Exception(MESSAGES_Exception[errorType]);
 }
 private void getMark() {
  // 设置初始值
  this.currentType = NULL_MARK;
  this.current = "";
  // 判断表达式是否结束,如果求值器当前指针等于字符串的长度则表示表达式已经结束
  if (this.position == this.expre.length()) {
   this.current = END;    // 如果表达式已经结束,则设置当前标记的置为END。
   return;
  }
  while (this.position < this.expre.length() // 当遇到表达式中的空白符则跳过
    && Character.isWhitespace(this.expre.charAt(this.position))) {
   ++this.position;
  }
  if (this.position == this.expre.length()) { // 判断当前表达式是否结束
   this.current = END;
   return;
  }
  char currentChar = this.expre.charAt(this.position); // 取得求值器当前指针指向的字符
  if (isSepara(currentChar)) {   // 如果当前字符是一个分隔符,则认为这是一个分隔符标记,
   this.current += currentChar;  // 给当前标记和标记类型赋值,并将指针后移
   this.position++;
   this.currentType = SEPARATION_MARK;
  } else if (Character.isLetter(currentChar)) {// 判断当前字符是否是一个字母
   while (!isSepara(currentChar)) { // 依次求值该变量的组成部分,直到遇到一个分隔符为止
    this.current += currentChar;
    this.position++;
    if (this.position >= this.expre.length()) {
     break;
    } else {
     currentChar = this.expre.charAt(this.position);
    }
   }
   this.currentType = VAR_MARK;  // 设置标记类型为变量
  } else if (Character.isDigit(currentChar)) { // 判断当前字符是否是一个数字
   while (!isSepara(currentChar)) { // 依次求值该数字的组成部分,直到遇到一个分隔符为止
    this.current += currentChar;
    this.position++;
    if (this.position >= this.expre.length()) {
     break;
    } else {
     currentChar = this.expre.charAt(this.position);
    }
   }
   this.currentType = NUM_MARK;  // 设置标记类型为数字
  } else {
   // 如果是无法识别的字符,则设置表达式结束
   this.current = END;
   return;
  }
 }
 private boolean isSepara(char c) {
  if ((" +-/*%^=()".indexOf(c) != -1))
   return true;
  return false;
 }
 public static void main(String[] args) throws Exception {
  StringDemo_24 sd24 = new StringDemo_24();
  double a = 15.0, b = 7.0;
  String express1 = "a = 15.0";
  System.out.println("表达式1:(\"a = 15.0\") = " + sd24.analysis(express1));
  String express2 = "b = 7.0";
  System.out.println("表达式2:(\"b = 7.0\") = " + sd24.analysis(express2));
  String express3 = "(a+b) * (a-b)";
  System.out.println("表达式3:(\"(" + a + "+" + b + ") * (" + a + "-" + b
    + ")\") = " + sd24.analysis(express3));
  String express4 = "2*6-7/2";
  System.out.println("表达式4:(\"2*6-7/2\") = " + sd24.analysis(express4));
  String express5 = "(8-3)*((a+b)/(a-b))";
  System.out.println("表达式5:(\"(8-3)*((" + a + "+" + "+" + b + ")/(" + a
    + "-" + b + "))\") = " + sd24.analysis(express5));
  String express6 = "13 % 2";
  System.out.println("表达式6:(\"13%2\") = " + sd24.analysis(express6));
  String express7 = "4^3 * 8 + 2";
  System.out
    .println("表达式7:\"3^2 * 5 + 4\") = " + sd24.analysis(express7));
 }
}
例如23:

package Chapter06.string;
public class StringDemo_23 {
 public static void main(String[] args) {
  String str = "Characters to find the specified location last seen";
  char c = 's';
  int sum = 0;
  for (int i = 0; i < str.length(); i++) {
   if (str.charAt(i) == c) {  // 从str字符串中依次截取字符与指定的字符相比较
    sum++;    // 如果相等sum自加1
   }
  }
  String pin = sum + "/" + str.length();
  System.out.println("字符" + c + "出现的频率为:\n            " + pin);
 }
}
例如22:

package Chapter06.string;
public class StringDemo_22 {
 static int[] a = null;
 static int b = 0;
 private static boolean isPrime(int n){//判断n是素数的方法
  for(int i=2;i<n;i++){
   if(n%i==0)
    return false;
  }
  return true;
 }
 public static void main(String[] args) {
  System.out.println("50和" + "300之间的所有素数如下:");
  for(int i=50;i<=300;i++){
   if(isPrime(i)==true){
    System.out.print(i+"\t");
    b++;
    if(b%5==0){
     System.out.println();
    }
   }
  }
 }
}
例如21:

package Chapter06.string;
public class StringDemo_21 {
 public static void main(String[] args) {
  String str = "Miss Li is a Teacher,Cycling to work every morning";
  System.out.println("原字符串如下:\n  " + str);
  char arr[] = str.toCharArray();      // 将字符串转化成char型数组
  for (int i = 0; i < arr.length; i++) {
   if (arr[i] >= 65 && arr[i] < 91) {     // 判断是否是大写字母
    arr[i] = (arr[i] + "").toLowerCase().charAt(0); // 如果是将当前字符的值由大写变小写
   } else if (arr[i] >= 97 && arr[i] < 123) {    // 判断是否是小写字母
    arr[i] = (arr[i] + "").toUpperCase().charAt(0); // 如果是将当前字符的值由小写变大写
   }
  }
  String s = new String(arr);       // 将char型数组转换成字符串
  System.out.println("大小写字母互换后的字符串如下:\n  " + s);
 }
}

原创粉丝点击