JAVA高级01_03 String类学习 2011-4-20

来源:互联网 发布:天猫历年双11数据分析 编辑:程序博客网 时间:2024/06/08 15:16

String类对象中的内容一旦被初始化就不能再改变。

 StringBuffer类用于封装内容可以改变的字符串。

 用toString方法转换成String类型

        String x = "a"+4+"c";编译等效于 String x = new StringBuffer.()append("a").append(4).append("c").toString().

字符串常量实际上是一种特殊的匿名String对象。

多个相同的字符串常量,java将只创建一个并共享

下面的s1 == s2 返回 true;

                     String s1 = "hello"; String s2 = "hello";

下面的s1 == s2 返回 false;

                    String s1 = new String("hello"); String s2 = new String("hello");

==用于比较两个引用是否指向同一个对象

比较内容可以使用s1.equals("hello");

System.in.read():从键盘中一次读取一个字节;

方法内部定义的变量必须显示的初始化。

 '/r'代表回车

'/n'代表换行

在windows操作系统中回车键代表为'/r'和'/n'

在Linux操作系统中回车键代表为'/r'

String类的常用成员方法构造方法:

      String(byte[] bytes,int offset,int length)

     equalslgnoreCase方法忽略大小写

     indexOf(int ch)方法返回指定字符在此字符串中第一次出现处的索引

     indexOf(int ch, int fromIndex)方法返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。

     substring(int beginIndex)从beginIndex位置返回后面的子字符串

     substring(int beginIndex,int endIndex)返回从beginIndex到endIndex-1之间的字符串