String

来源:互联网 发布:淘宝达人怎样申请大v 编辑:程序博客网 时间:2024/05/01 15:59
String类的使用:(字符串)
一.两种实例化方法:
(1).Strirng 变量 = ".........";(直接实例化方法)
(2).String 变量=new String ("..........");()
注:String的构造方法:public String (String str){......}

(1)直接初始化:共享设计模式:在JVM的底层实际上会有一个对象池,当用直接赋值定义了一个String类对象时,会将对象入池保存,
当后续再次用直接赋值定义了一个String对象时,就不会再次申请堆内存,而是要将对象池中的String对象引用赋值
给该变量,从而继续使用。
(2)采用构造方法实例化:使用构造方法创建String对象,则是先创建匿名对象,然后在new一个String对象,然后将new的对象赋值给
变量名。而匿名对象就成了垃圾。(即开辟两块堆内存空间,其中一块将成为垃圾)
对象不保存在对象池中,但是可以使用intern()手工入池;
范例:
import javax.swing.JOptionPane;import java.util.Scanner;public class project_1{public static void main(String args[]){String a=new String ("a");String b="a";System.out.println(a==b);String c=new String ("b").intern() ;String d="b";System.out.println(d==c);}}

二.字符串的比较:字符串是不可以通过大于,小于符号来比较的,而同过等于符号,比较的只是地址是否相等。
要想比较String中的内容:
·对象1.equals(对象2);(String中定义了比较的函数,所以要通过对象来调用)


三.字符串常量就是String的匿名对象。匿名对象是系统自己生成的,而不是用户自己定义的。
注意:在使用输入函数时,如果输入为空,则null.equals("..."),可能会出现空指向异常。


四字符串常量一旦定义就不可改变。
import javax.swing.JOptionPane;import java.util.Scanner;public class project_1{public static void main(String args[]){String str="Hello ";str+="world";//会先在堆内存中申请一个world,然后在申请一个 hello world的堆内存,再将str指向hello worl    //实际上字符串并不会改变,改变的只是str所指的地址。(会一直产生垃圾空间)System.out.println(str);}}
String 中的操作方法:1.字符和字符串
public class project_1{public static void main(String args[]){char arr[]={'a','b','c','4','5'};String str1=new String(arr);//String(char[] value) String str2=new String(arr,1,3);//String(char[] value, int offset, int count) char a=str2.charAt(0);//public char charAt(int index)char arr2[]=str1.toCharArray();//public char[] toCharArray()System.out.println(str1);System.out.println(str2);System.out.println(a);System.out.println(arr2[1]);}}

2.字节和字符串(主要在IO操作时会用到)
字节主要用于数据传输和编码转换问题:
public class project_1{public static void main(String args[]){byte a[]={48,49,50,51,52,53};String str1=new String (a);//String(byte[] bytes) String str2=new String (a,0,3);//String(byte[] bytes, int offset, int length) byte b[]=str2.getBytes();//public byte[] getBytes()//public byte[] getBytes(String charsetName)  用于编码转换        //        throws UnsupportedEncodingExceptionSystem.out.println(str1);System.out.println(str2);System.out.println(b[0]);}}

3.字符串的比较
 boolean  equals(Object anObject) 
          将此字符串与指定的对象比较。
 boolean  equalsIgnoreCase(String anotherString) 
          将此 String 与另一个 String 比较,不考虑大小写。

public int compareTo(String anotherString):如果按字典顺序此 String 对象位于参数字符串之前,则比较结果为一个负整数。
如果按字典顺序此 String 对象位于参数字符串之后,则比较结果为一个正整数。如果这两个字符串相等,则结果为 0;
(即:做差,对象减去参数对象)
范例:(只有String类具有此功能)
public class project_1{public static void main(String args[]){String a="hello";String b="Hello";System.out.println(a.compareTo(b));}}
4.字符串查找
public boolean contains(CharSequence s)
当且仅当此字符串包含指定的 char 值序列时,返回 true。
public int indexOf(int ch) (查找不到返回-1)
          返回指定字符在此字符串中第一次出现处的索引。
public int indexOf(int ch, int fromIndex) 
          返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
public int indexOf(String str) 
          返回指定子字符串在此字符串中第一次出现处的索引。
public int indexOf(String str, int fromIndex) 
          返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
public int lastIndexOf(int ch) 
          返回指定字符在此字符串中最后一次出现处的索引。
public int lastIndexOf(int ch, int fromIndex) 
          返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
public int lastIndexOf(String str) 
          返回指定子字符串在此字符串中最右边出现处的索引。
public int lastIndexOf(String str, int fromIndex) 
          返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
范例:
public class project_1{public static void main(String args[]){String a="hello";char b='l';System.out.println(a.indexOf(b));}}

public boolean endsWith(String suffix) 
          测试此字符串是否以指定的后缀结束。
public boolean startsWith(String prefix) 
          测试此字符串是否以指定的前缀开始。
public boolean startsWith(String prefix, int toffset) 
          测试此字符串从指定索引开始的子字符串是否以指定前缀开始。


范例:
public class project_1{public static void main(String args[]){String a="hihihello";String b="hi";System.out.println(a.startsWith(b));}}

5.字符串的替换
 String replace(char oldChar, char newChar) 
          返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
 String replace(CharSequence target, CharSequence replacement) 
          使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
 String replaceAll(String regex, String replacement) 
          使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
 String replaceFirst(String regex, String replacement) 
          使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。


6.字符串的截取(JS中Index可以是负数,而在java中Index只可以是正数)
 String substring(int beginIndex) 
          返回一个新的字符串,它是此字符串的一个子字符串。
 String substring(int beginIndex, int endIndex) 
          返回一个新字符串,它是此字符串的一个子字符串。
范例:
public class project_1{public static void main(String args[]){String a="hihihello";String b=a.substring(4);String c=a.substring(0,2);System.out.println(b);System.out.println(c);}}

7.字符串拆分(正则)
String[] split(String regex) 
          根据给定正则表达式的匹配拆分此字符串。
String[] split(String regex, int limit) (limit用来表示拆分后的数组最多有几个)
          根据匹配给定的正则表达式来拆分此字符串。
范例:
public class project_1{public static void main(String args[]){String a="1hi2hihello";String [] b=a.split("hi");for(int x=0;x<b.length;x++){System.out.println(b[x]);}String []c=a.split("hi",2);for(int x=0;x<c.length;x++){System.out.println(c[x]);}}}
8.其他方法String concat(String str)           将指定字符串连接到此字符串的结尾。如果参数字符串的长度为 0,则返回此 String 对象。否则,创建一个新的 String 对象,用来表示由此 String 对象表示的字符序列和参数字符串表示的字符序列连接而成的字符序列。范例:
public class project_1{public static void main(String args[]){String str1="Hello ";String str2=str1+"world";String str3="Hello world";String str4=str1.concat("world");String str5=str3.concat("");System.out.println(str2==str3);//falseSystem.out.println(str2==str4);//falseSystem.out.println(str4==str3);//falseSystem.out.println(str4==str5);//false}}

String toLowerCase() 
          使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
String toLowerCase(Locale locale) (根据用户的定义来转换)
          使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
String toUpperCase() 
          使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
String toUpperCase(Locale locale) 
          使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。
String trim() 
          返回字符串的副本,忽略前导空白和尾部空白。
public String intern()
当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(用 equals(Object) 方法确定),
则返回池中的字符串。否则,将此 String 对象添加到池中,并返回此 String 对象的引用。
0 0