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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王楚鑫 中级黑马   /  2013-6-25 13:17  /  964 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

刚刚复习到字符串变量、、分享一下练习的代码、、常用的声明和操作的方法、、、进度差不多的童鞋来讨论下还有什么该狠狠记住的呢、、
  1. public class Stringex{
  2.         public static void main(String[] args)
  3.         {
  4.                 //----------声明创建字符串变量 ---------------
  5.                 //(1).可以为字符串变量赋值简单的字符串常量来完成初始化
  6.                 String str1="My number";
  7.                 System.out.println(str1);
  8.                
  9.                 //(2). String()构造方法
  10.                 String str2;
  11.                 str2=new String("  is ");
  12.                 System.out.println(str2);
  13.                
  14.                 //(3).  String(byte[ ] bytes)构造方法
  15.                 byte[] byteArray=new byte[]{52,49,51}; // 创建字节数组
  16.                 String str3=new String(byteArray);
  17.                 System.out.println(str3);
  18.                
  19.                 //(4). String(String  charsetName)构造方法
  20.                 String str4=new String(". ");
  21.                 System.out.println(str4);
  22.                
  23.                 //-----------字符串操作---------------------------
  24.                 //(1). 获取字符串长度
  25.                 System.out.println("(1).The str1's length:"+str1.length());
  26.                
  27.                 //(2). 字符串查找
  28.                 //indexOf(String s) 返回指定子字符串在此字符串中第一次出现处的索引
  29.                 System.out.println("(2).The letter 'e' 's index:  "+str1.indexOf("n"));

  30.                 //(3). lastIndexOf(String str) 返回指定子字符串在此字符串中最右边出现处的索引。
  31.                 System.out.println("(3).The letter 'r' 's the right index:  "+str1.indexOf("r"));
  32.                
  33.                 //(4). 获取指定索引位置的字符
  34.                 //str.charAt(int index)
  35.                 System.out.println("(4). The index of 3's letter is:  "+str1.charAt(3));
  36.                
  37.                 //(5). 获取子字符串
  38.                 //substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。
  39.                 System.out.println("(5).Behind the index of 3's substring is:  "+str1.substring(3));
  40.                 //substring(int beginIndex , int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。
  41.                 System.out.println("(5).Between the index  5 and 8's substring is:  "+str1.substring(5,8));
  42.                
  43.                 //(6). 去除空格
  44.                 //trim()方法返回字符串的副本,忽略前导空白和尾部空格
  45.                 System.out.println("(6).After delete space,the new string is:"+str2.trim()+str3);
  46.                
  47.                 //(7).字符串替换
  48.                 //str.replace(char oldChar,char newChar); replace()方法可实现将指定的字符或字符串替换成新的字符或字符串
  49.                 System.out.println("(7).The new String is:  "+str1.replace("My","my"));
  50.                
  51.                 //(8). 判断字符串是否相等str.equals(String otherstr)  
  52.                 //如果两个字符串具有相同的字符和长度,则使用equals()方法进行比较时,则返回true。
  53.                 System.out.println("(8)."+str3.equals("413"));
  54.                
  55.                 //(9).字母大小写转换
  56.                 System.out.println("(9)."+str1.toUpperCase());
  57.                 System.out.println("MY name IS".toLowerCase());
  58.                
  59.                 //(10).字符串分割str.split(String sign)
  60.                 String str5="Today is Tuesday.";
  61.                 String[] newstr=str5.split(" ");
  62.                 System.out.println("(10).The new string is:");       
  63.                 for(int i=0;i<newstr.length;i++)
  64.                 {
  65.                         System.out.println(newstr[i]);
  66.                 }                       
  67.         }
  68. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1 赞一个!

查看全部评分

1 个回复

倒序浏览
字符串是非常重要的,一定要弄懂弄会才行
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马