黑马程序员技术交流社区

标题: 今天学了String [打印本页]

作者: 夏至落叶    时间: 2014-11-19 22:19
标题: 今天学了String
今天学习了String,对于第一次接触API的我i感觉这一天学到了很多方法,但是最后感觉白天学的都忘得差不多了,晚自习就开始敲代码,把讲到的String方法都敲一遍,顺便总结。
把总结打包了,里面有我得总结和总结敲的小代码。还有老师上课说的例题.
学习java就是要敲代码。特别是API,不会就敲个几遍。都是很基础的,高手勿喷哈
  1. //String
  2. //构造函数直接看API,就不总结了。

  3. /* 判断功能:
  4. * boolean equals(Object obj):判断字符串的内容是否相同,区分大小写。
  5. * boolean equalsIgnoreCase(String str):判断字符串的内容是否相同,不区分大小写。
  6. * boolean contains(String str):判断字符串对象是否包含给定的字符串。
  7. * boolean startsWith(String str):判断字符串对象是否以给定的字符串开始。
  8. * boolean endsWith(String str):判断字符串对象是否以给定的字符串结束。
  9. * boolean isEmpty():判断字符串对象是否为空。数据是否为空。
  10. */
  11. public class StringDemo {
  12.         public static void main(String[] args) {
  13.                 String s = "helloword";
  14.                 String s2 = "helloword";
  15.                 String s3 = "Helloword";
  16.                 String s4 = "ow";
  17.                 String s5 = "";
  18.                 System.out.println(s.equals(s2));
  19.                 System.out.println(s.equalsIgnoreCase(s3));
  20.                 System.out.println(s.contains(s4));
  21.                 System.out.println(s.startsWith("h"));
  22.                 System.out.println(s.endsWith("rd"));
  23.                 System.out.println(s5.isEmpty());
  24.         }
  25.        

  26. /* * 字符串的获取功能:
  27. * int length():获取字符串的长度
  28. * char charAt(int index):返回字符串中给定索引处的字符
  29. * int indexOf(int ch):返回指定字符在此字符串中第一次出现的索引
  30. * int indexOf(String str):返回指定字符串在此字符串中第一次出现的索引
  31. * int indexOf(int ch,int fromIndex):返回在此字符串中第一次出现指定字符的索引,从指定的索引开始搜索。
  32. * int indexOf(String str,int fromIndex):返回在此字符串中第一次出现指定字符串的索引,从指定的索引开始搜索。
  33. * String substring(int start):截取字符串。返回从指定位置开始截取后的字符串。
  34. * String substring(int start,int end)截取字符串。返回从指定位置开始到指定位置结束截取后的字符串。
  35. */
  36. public class StringDemo2 {
  37.         public static void main(String[] args) {
  38.                 String str = "helloword";
  39.                 int s = str.length();
  40.                 char s1 = str.charAt(3);
  41.                 int s2 = str.indexOf('w');
  42.                 int s3 = str.indexOf("low");
  43.                 int s4 = str.indexOf('o',3);
  44.                 int s5 = str.indexOf("or", 1);
  45.                 String s6 = str.substring(3);
  46.                 String s7 = str.substring(1, 5);
  47.                 System.out.println(s);
  48.                 System.out.println(s1);
  49.                 System.out.println(s2);
  50.                 System.out.println(s3);
  51.                 System.out.println(s4);
  52.                 System.out.println(s5);
  53.                 System.out.println(s6);
  54.                 System.out.println(s7);
  55.         }


  56. //字符串的转换功能:
  57. /*
  58. * byte[] getBytes():把字符串转换成字节数组。
  59. * char[] toCharArray():把字符串转换成字符数组。
  60. * static String copyValueOf(char[] chs):把字符数组转换成字符串。
  61. * static String valueOf(char[] chs):把字符数组转换成字符串。
  62. * static String valueOf(int i)基本类型:把int(基本类型)转换成字符串。
  63. * String toLowerCase():把字符串变成小写
  64. * String toUpperCase():把字符串变成大写
  65. * String concat(String str):拼接字符串。
  66. */
  67. public class StringDemo3 {
  68.         public static void main(String[] args) {
  69.                 String str = "abcdefg";
  70.                 byte [] s = str.getBytes();
  71.                 for (int i = 0; i < s.length; i++) {
  72.                         System.out.print(s[i]+" ");
  73.                 }
  74.                 System.out.println("");
  75.                 char[] s1 = str.toCharArray();
  76.                 for (int i = 0; i < s.length; i++) {
  77.                         System.out.print(s1[i]);
  78.                 }
  79.                 System.out.println("");
  80.                 char [] s3 = { 'a', 'b', 'c', '中', '国'};
  81.                 //System.out.println(s);
  82.                 String s4 = String.copyValueOf(s3);
  83.                 System.out.println(s4);
  84.                 String s5 = String.valueOf(158);
  85.                 String s6 = str.toUpperCase();
  86.                 String s7 = str.toLowerCase();
  87.                 String s8 = str.concat(s5);
  88.                 System.out.println(s6);
  89.                 System.out.println(s7);
  90.                 System.out.println(s8);
  91.         }
  92. }


  93. /*
  94. *  替换功能
  95. *         String replace(char oldChar,char newChar):用新的字符去替换指定的旧字符
  96. *        String replace(String oldString,String newString):用新的字符串去替换指定的旧字符串
  97. *
  98. * 切割功能
  99. *         String[] split(String regex)
  100. *
  101. * 去除字符串两端空格       
  102. *         String trim()
  103. *
  104. * 按字典顺序比较两个字符串  
  105. *        int compareTo(String str)
  106. */
  107. public class StringDemo4 {
  108.         public static void main(String[] args) {
  109.         String str = "hello";
  110.         String s7 = "abllo";
  111.         String s6 = " hello java ";
  112.         String s9 = "abcdefghadfjas";
  113.         String s = str.replace('l', 'p');
  114.         String s1 = str.replaceAll("lo", "pk");
  115.         String[] s4 = s9.split("f");
  116.         String s5 = s6.trim();
  117.         int x = str.compareTo(s7);
  118.         System.out.println(s);
  119.         System.out.println(s1);
  120.         for (int i = 0; i < s4.length; i++) {
  121.                 System.out.println(s4[i]);
  122.         }
  123.        
  124.         System.out.println(s5);
  125.         System.out.println(x);
  126.         }
  127. }

  128. 课堂老师说的练习题:
  129. import java.util.Scanner;

  130. /*
  131. * 输入字母首字母大写后面字母小写的功能
  132. *
  133. *
  134. */
  135. public class StringTest {
  136.         public static void main(String[] args) {
  137.                 Scanner sc = new Scanner(System.in);
  138.                 System.out.println("输入字母");
  139.                 String s = sc.next();
  140.                 //获取字符串第一个字母并大写
  141.                 String s1 = s.substring(0,1).toUpperCase();
  142.                 //获取字符串除第一个字母并小写
  143.                 String s2 = s.substring(1).toLowerCase();
  144.                 //连接字符串
  145.                 String s3 = s1.concat(s2);
  146.                 System.out.println(s3);
  147.                 System.out.println("********************");
  148.                 //链式写法
  149.                 String s4 = s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase());
  150.                 System.out.println(s4);
  151.         }
  152. }       



  153. import java.util.Scanner;
  154. /*
  155. * 输入字符串
  156. * 分别算出字符串中大写字母,小写字母,数字的个数
  157. *
  158. */
  159. public class Test {
  160.         public static void main(String[] args) {
  161.                 //定义3个变量,分别是大写字母个数,小写字母个数,数字个数
  162.                 int bigcount = 0;
  163.                 int smallcount = 0;
  164.                 int number = 0;
  165.                 //键盘录入功能
  166.                 Scanner sc = new Scanner(System.in);
  167.                 System.out.println("输入字符串");
  168.                 String s = sc.nextLine();
  169.                
  170.                 for (int i = 0; i <s.length(); i++) {
  171.                         //把字符串转化为字符数组
  172.                         //进行遍历
  173.                         char ch = s.charAt(i);
  174.                         //判断数组中各个数字类型并累加
  175.                         if (ch>=65&&ch<=90) {
  176.                                 bigcount++;
  177.                         } else if(ch>=97&&ch<=122){
  178.                                 smallcount++;
  179.                         }
  180.                         else if(ch>=48&&ch<=57){
  181.                                 number++;
  182.                         }
  183.                 }
  184.                 System.out.println("大写字母个数:"+bigcount);
  185.                 System.out.println("小写字母个数:"+smallcount);
  186.                 System.out.println("数字个数:"+number);
  187.                
  188.         }
  189. }



  190. /*
  191. * /*
  192. * 获取一个字符串中指定子串出的次数。比如说“hanbasdnbafllgnbahjnbakqqqqlnbaxi” 在这个字符串中,多有个nba?
  193. *
  194. *
  195. */
  196. public class Test2 {
  197.         public static void main(String[] args) {
  198.                 String s = "hanbasdnbafllgnbahjnbakqqqqlnbaxi";
  199.                 String x = "nba";
  200.                 int count = fun(s,x);
  201.                 System.out.println(count);
  202.                
  203.         }
  204.        
  205.         public static int fun(String maxstring,String minstring){
  206.                 int count = 0;
  207.                 //找到小字符串在大字符串第一次出现的位置
  208.                 int index = maxstring.indexOf(minstring);
  209.                 //当字符串中没有指定子串返回-1,下面则直接退出
  210.                 while (index!=-1){
  211.                         count++;
  212.                         //把小字符串前的字符删掉重新组成新字符
  213.                         maxstring = maxstring.substring(index+minstring.length());
  214.                         //再找出小字符串的位置
  215.                         index = maxstring.indexOf(minstring);
  216.                 }
  217.                 return count;
  218.         }
  219. }
复制代码



作者: 天天小志    时间: 2014-11-19 22:27
先来占个位子,200多行,真猛!
作者: 这是我的地盘    时间: 2014-11-19 22:28
做的很好,程序的学习过程就是从敲代码开始的,多敲多练,总会有收获的,赞一个!
作者: wxnzb502    时间: 2014-11-19 22:32
记录的不错
作者: 我就是姚舜禹    时间: 2014-11-19 22:33
做的很好,程序的学习过程就是从敲代码开始的,多敲多练,总会有收获的,赞一个!
作者: NCry    时间: 2014-11-19 22:35
200多行。 这么多行就应该赞啊!!!!
作者: 随风sky    时间: 2014-11-19 22:41
真给力!加油,哥们!
作者: 迷失的小Z    时间: 2014-11-19 22:44
每一天都敲200行,你离牛人也不远了……
作者: zbt    时间: 2014-11-19 22:48
没抢到沙发
作者: 夏至落叶    时间: 2014-11-19 22:49
中间不少是注释,没有200行。玩java就是敲代码。就这样上面的代码我还没记住。明天还得继续敲
作者: a986875894    时间: 2014-11-19 22:53
本帖最后由 a986875894 于 2015-12-10 21:08 编辑

..................
作者: 王小忠    时间: 2014-11-20 00:40
厉害~~200多行,以后多多交流哈
作者: 梦浮冀北    时间: 2014-11-20 09:18
不错,继续加油




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2