从屌丝到架构师的飞越(API篇)-String类

一、介绍

字符串广泛应用在Java 编程中,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。 String类是不可变的,对String类的任何改变,都是返回一个新的String类对象。 String 对象是 System.Char 对象的有序集合,用于表示字符串。String 对象的值是该有序集合的内容,并且该值是不可变的。

字符串或串(String)是由数字、字母、下划线组成的一串字符。一般记为 s=“a1a2···an”(n>=0)。它是编程语言中表示文本的数据类型。在程序设计中,字符串(string)为符号或数值的一个连续序列,如符号串(一串字符)或二进制数字串(一串二进制数字)。

通常以串的整体作为操作对象,如:在串中查找某个子串、求取一个子串、在串的某个位置上插入一个子串以及删除一个子串等。两个字符串相等的充要条件是:长度相等,并且各个对应位置上的字符都相等。设p、q是两个串,求q在p中首次出现的位置的运算叫做模式匹配。串的两种最基本的存储方式是顺序存储方式和链接存储方式。

二、知识点介绍

1、String类

2、String构造方法

3、String常用方法

三、上课对应视频的说明文档

1、String类

String 类用于比较两个字符串,查找和抽取串中的字符或子串、字符串与其他类型之间相互转换等。String 类是一个常量对象,String 类对象的内容一旦被初始化就不能再被改变。

2、String构造方法

➢ public Stirng()

创建一个字符串对象,其字符串值为空。

➢ public String (String value)

用字符串对象value创建一个新的字符串对象。

➢ public String (char[] value)

用字符数组value来创建字符串对象。

➢ public String (char[]value,int offset,int count)

从字符数组value中下标为offset的字符开始,创建有count个字符的字符串对象。

➢ public String(byte ascII[])

用byte型字符串数组ascII,按缺省的字符编码方案创建字符串对象。

➢ public String(byte ascII[],int offset int count))

从字节型数组ascII中下标为offset的字符开始,按缺省的字符编码方案创建count个字符的串对象。

➢ public String(StringBuffer buffer)

用缓冲字符串buffer创建一个字符串对象。

例:

StringTest1.java类

public class StringTest1{

public static void main(String args[]){

String s1="hello";

String s2="world";

String s3="hello";

System.out.println(s1==s3);

s1=new String ("hello");

s2=new String ("hello");

System.out.println(s1==s2);

System.out.println(s1.equals(s2));

char c[]={'s','u','n',' ','j','a','v','a'};

String s4=new String(c);

String s5=new String(c,4,4);

System.out.println(s4);

System.out.println(s5);

}

}

3、String常用方法

(1)常用方法1

➢ public char charAt(int index)

返回字符串中第index个字符。

➢ public int length()

返回字符串的长度。

➢ public int indexOf(String str)

返回字符串中出现str 的第一个位置。

➢ public int indexOf(String str,int fromIndex)

返回字符串中从fromIndex开始出现str的第一个位置。

➢ public boolean equalsIgnoreCase(String another)

比较字符串是否与another一样。

➢ public String replace(char oldChar,char newChar)

字符串中用newchar替代oldChar字符。

例:

StringTest2.java类

public class StringTest2{

public static void main(String args[]){

String s1="sun java";

String s2="sun Java";

System.out.println(s1.charAt(1));

System.out.println(s2.length());

System.out.println(s1.indexOf("java"));

System.out.println(s1.indexOf("Java"));

System.out.println(s1.equals(s2));

System.out.println(s1.equalsIgnoreCase(s2));

String s="我是程序员,我在学java";

String sr=s.replace('我','你');

System.out.println(sr);

}

}

(2)常用方法2

➢ public boolean startsWith(String prefix)

判断字符串是否以prefix字符串开头。

➢ public boolean endsWith(String suffix)

判断字符串是否以suffix字符结尾。

➢ public String toUpperCase()

返回一个字符串为该字符串的大写形式。

➢ public String toLowerCase()

返回一个字符串为该字符串的小写形式。

➢ public String substring(int beginIndex)

返回字符串从beginIndex开始到结尾的子字符串。

➢ public String substring(int beginIndex,int endIndex)

返回字符串从beginIndex开始到endIndex结尾的子字符串。

➢ public String trim()

返回将该字符串去掉开头和结尾空格后的字符串。

例:

StringTest3.java类

public class StringTest3{

public static void main(String args[]){

String s="Welcome to Java World";

String s1="  sun java  ";

System.out.println(s.startsWith("Welcome"));

System.out.println(s.endsWith("World"));

String sL=s.toLowerCase();

String sU=s.toUpperCase();

System.out.println(sL);

System.out.println(sU);

String subS=s.substring(11);

System.out.println(subS);

String sp=s1.trim();

System.out.println(sp);

}

}

(3)常用方法3

静态重载方法:

➢ public static String valueOf(...)可以将基本类型数据转换为字符串:

例如:

public static String valueOf(double d)

public static String valueOf(int i)

....

➢ public String []split(String regex)可以将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组。

例:

StringTest4.java类

public class StringTest4{

public static void main(String args[]){

int j=1234567;

String sNumber=String.valueOf(j);//转换成String类型

System.out.println("j 是"+sNumber.length()+"位数。");

String s ="Mary,F,1976";

String sPlit[] =s.split(",");//以,号分隔

for(int i=0;i

System.out.println(sPlit[i]);

}

}

}

(4)常用方法4

➢ concat(String str):把字符串str附加在当前字符串的末尾。

例如:

String str="Hello";

String newStr=str.concat("World");

System.out.println(str);  //打印Hello

System.out.println(newStr);  //打印HelloWorld

*:以上concat()方法并不会改变字符串str本身的内容。

public class StringDemo {

public static void main(String[] args) {

// print str1

String str1 = "self";

System.out.println(str1);

// print str2 concatenated with str1

String str2 = str1.concat(" learning");

System.out.println(str2);

// prints str3 concatenated with str2(and str1)

String str3 = str2.concat(" center");

System.out.println(str3);

}

}

你可能感兴趣的:(从屌丝到架构师的飞越(API篇)-String类)