A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© adalvik 中级黑马   /  2015-4-12 23:46  /  937 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


下面是题目

题目一:写一个正则表达式,可以匹配手机号。规则: 第1位是1,第二位可以是数字3458其中之一,后面4位任意数字,最后5位为任意相同的数字。例如:18601088888、13912366666

题目二:编写程序,打印1到100之内的整数,但数字中包含7的要跳过,例如:17、27、71、72

题目三:编写程序,从一个字符串中按字节数截取一部分,但不能截取出半个中文,例如:从“hello枷哇”中截取2个字节是“he,截取6个字节也要是"hello",而不要出现半个中文。

下面是代码

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

  3. class main {

  4. public static void main(String[] args) {
  5. String number = "de1314875111111111";
  6. System.out.println("题目1开始");
  7. System.out.println(matcherPhone(number));
  8. System.out.println("题目1结束\n");

  9. System.out.println("题目2开始");
  10. showNum();
  11. System.out.println("题目2结束\n");


  12. String a = "11我是世界";
  13. System.out.println("题目3开始");
  14. System.out.println(substring(a, 3));
  15. System.out.println("题目3结束");
  16. }

  17. /**
  18. * 题目一:写一个正则表达式,可以匹配手机号。规则: 第1位是1,第二位可以是数字3458其中之一,后面4位任意数字,最后5位为任意相同的数字。
  19. * 题目一
  20. *
  21. * @param number
  22. * 手机号码
  23. * @return 返回匹配的第一个号码.
  24. */
  25. public static String matcherPhone(String number) {

  26. if (number == null || number.length() < 11) {
  27. return "";
  28. }

  29. Pattern pattern = Pattern.compile("1[3,4,5,8]\\d{4}([0-9])\\1{4}");
  30. Matcher matcher = pattern.matcher(number);
  31. String phone = "";
  32. if (matcher.find()) {
  33. int gc = matcher.groupCount();

  34. if (gc > 0) {
  35. phone = (matcher.group(0));

  36. }

  37. }

  38. return phone;

  39. }

  40. /**
  41. * 题目二:编写程序,打印1到100之内的整数,但数字中包含7的要跳过,例如:17、27、71、72
  42. */
  43. public static void showNum() {

  44. for (int i = 0; i <= 100; i++) {
  45. String temp = Integer.toString(i);
  46. if (temp.indexOf("7") == -1) {
  47. System.out.print(i+",");
  48. }

  49. }
  50. System.out.print("\n");
  51. }

  52. /**
  53. * 题目三:编写程序,从一个字符串中按字节数截取一部分,但不能截取出半个中文,例如:从“hello枷哇”中截取2个字节是“he,截取6个字节也要是
  54. * "hello",而不要出现半个中文。
  55. *
  56. * @param text
  57. * @param length
  58. * @return
  59. */
  60. public static String substring(String text, int length) {
  61. if (text == null) {
  62. return null;
  63. }
  64. StringBuilder sb = new StringBuilder();
  65. int currentLength = 0;
  66. try {
  67. for (char c : text.toCharArray()) {
  68. currentLength += String.valueOf(c).getBytes("GB2312").length;
  69. if (currentLength <= length) {
  70. sb.append(c);
  71. } else {
  72. break;
  73. }
  74. }

  75. } catch (Exception e) {
  76. // TODO: handle exception
  77. }

  78. return sb.toString();

  79. }

  80. }
复制代码

PS:好想报android班  可惜技术和积分都不够呀~~新的一周开始了,大家加油~~~








评分

参与人数 1黑马币 +5 收起 理由
label + 5 很给力!

查看全部评分

6 个回复

倒序浏览
居然都会还说技术不够。。。我只会第二题
回复 使用道具 举报
刚好学习下了
回复 使用道具 举报
慢慢学来
回复 使用道具 举报
代码不错
回复 使用道具 举报
代码不错
回复 使用道具 举报
0.0  学到新东西了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马