JAVA之字符串操作

一、字符串分类

       JAVA提供了两种字符串类:String类和StringBuffer类。

String类:该类一旦产生一个字符串,其对象就不可变。String的内容和长度是固定的。虽然通过各种系统方法可以对字符串施加操作,但这并不改变对象实例本身,而是生成一个新的实例(重新赋值其实是两个对象)。系统为String类对象分配内存,是按照对象包含的实际字符数分配的。

StringBuffer类用来处理可变字符串。如果要修改一个StringBuffer类的字符串,不需要再创建新的字符串对象,而是直接操作原来的字符串。系统为StringBuffer类对象分配内存时,除去当前字符所占空间外,还提供另外16个字符大小的缓冲区。注意使用StringBuffer类对象时,使用length()方法获得市级包含字符串的长度,capacity()方法返回当前数据容量和缓冲区的容量之和。

二、字符串的声明及创建

    1)字符串的声明格式有两种:常量声明方式和对象声明方式

     例如:String stringhello="hello";

                 String stringhello = new String("hello");

    2) String类字符串的创建的几种方式

       (1)初始化一个新创建的String对象,该对象表示一个空(null)的字符串

              String s = new String();

       (2)初始化一个新创建的String对象,该对象初始化为参数String表示的字符串。

              String s = new String(String);

       (3)初始化一个新创建的String对象,该对象表示字符数组顺序组成的字符串。

              String s = new String(char chars[]);

       (4)初始化一个新创建的String对象,该对象表示由字符数组的部分组成的字符串,第2个参数表示部分字符数组的起始位置,第3个参数表示从起始位置开始的字符数        量。

             String s = new String(char chadrs[],int startindex,int numchars)

       (5)初始化一个新创建的String对象,该对象表示由Unicode codepoint数组的一个子数组组成的字符串。Unicode编码是单字节,即一个字符用一个字节表示

            String s = new String(int[],int begin,int end)

       (6)初始化一个新创建的String对象,该对象的内容是StringBuffer类所包含的字符数组

             String s = new String(StringBuffer)

   3) StringBuffer类字符串创建的几种方式

       (1)初始化一个新创建的StringBuffer对象,该对象不包含任何字符,初始化容量为16个字符

             String s = new StringBuffer()

       (2)初始化一个新创建的StringBuffer对象,该对象不包含任何字符,初始化容量为“length”个字符,即初始化时确定了对象的容量。

              String s = new StringBuffer(int length)

        (3)初始化一个新创建的StringBuffer对象,该对象初始化为参数String的内容

              String s = new StringBuffer(String)

        (4)初始化一个新创建的StingBuffer对象,该对象初始化为参数CharSequence的内容

              String s = new StringBuffer(CharSequence)的内容

               --CharSequence是一个接口,其本质上就是一个字符序列,String和StringBuffer都实现了这个接口

三、字符串操作

       1、字符串连接concat()

       

public class TestString{
public static void main( String args[]){
String s1 = new String( "hello");
String s2 = new String( "world");
String s3 = s1. concat(s2);
System. out. println( "s3= "+s3);
}
}
结果是:helloworld
2、比较字符串equals()
比较字符串实现返回两个字符串内容是否相同的比较结果,如果相同则返回true,如果不同则返回false。Java调用String类的equals()函数
实现,该函数的参数是一个String对象,返回值是boolean值(比较后的结果)。equals()函数是String类独有的。
public class TestString{
public static void main( String args[]){
String s1 = new String( "hello");
String s2 = new String( "world");
String s3 = s1. concat(s2);
String s4 = new String( "helloworld");
System. out. println( s1. equals(s2));
System. out. println( s3. equals(s4));
}
}
结果是:false true
3、获取字符串的长度length()和capacity()
函数length()来获取当前对象中的字符数量,但是StringBuffer类还提供了capacity()函数说明字符容量,因为有缓冲区,所以容量大小
肯定大于当前对象中的字符数量。length()函数是String类和StringBuffer类共有的,而capacity()函数是StringBuffer类独有的。
public class TestString{
public static void main( String args[]){
String s1 = new String( "hello");
StringBuffer sf = new StringBuffer( "world");
System. out. println( s1. length());
System. out. println( sf. length());
System. out. println( sf. capacity());
}
}
结果是: 5 5 21
4.复制字符串copyValueOf()
copyValueOf()是String类独有的
String copyValueOf(char[], int offset,int count)
该参数方式复制部分字符数组的内容为一个新字符串
String copyValueOf(char[])
该参数方式复制整个字符数组为一个新的字符串。
public class TestString{
public static void main( String args[]){
String s1 = new String( "hello");
char [] s_char = new char[ s1. length()];
for( int i = 0;i< s1. length();i++){
s_char[i]= s1. charAt(i);
}
String s2 = String. copyValueOf(s_char);
String s3 = String. copyValueOf(s_char, 0, 3);
System. out. println(s1);
System. out. println(s2);
System. out. println(s3);
}
}

结果是:hello hello hel
5.获得子串subString()
String substring(int start,int end)
该参数方式获得字符串中从start到end(不包含end位置)的字符串
String substring(int count)
该参数方式获得字符串中从count(包含count位置)开始到字符串结束的子串。
如果参数index的长度超过了字符串的长度范围,则编译系统抛出异常:java.lang.StringIndexOutOfBoundsException.
实现方式:用参数值减去字符串长度,如果小于0就抛出异常,且输出差值。如果参数本身就是负值(如输入错误等),则抛出异常且直接输出
该负数值。substring()函数是String类独有的。

public class TestString{
public static void main( String args[]){
String s1 = new String( "hello");
String s2 = s1. substring( 0, 2);
String s3 = s1. substring( 2);
System. out. println(s1);
System. out. println(s2);
System. out. println(s3);
}
}

结果是:hello he ll0
6.获取指定位置的字符charAt()
charAt()函数是String类和StringBuffer类共有的
String charAt(int index)

public class TestString{
public static void main( String args[]){
String s1 = new String( "hello");
System. out. println( s1. charAt( 2));
}
}
结果是:l
7.更改大小写toUpperCase()、toLowerCase()
String s1 = new String("hello");
String upper1 = s1.toUpperCase();
String s2 = new String("HELLO");
String lower = s2.toLowerCase();

8.分割字符串split(String)
分割字符串就是按照指定的划界表达式把字符串分割成几部分,每部分都是一个字符串,方法返回值是字符串数组(String[])。split(String)函数是String类独有的。
String s1 = new String("hello");
String[] splitresult = s1.split("l");//划界表达式是"l",分割结果是("he","","o")

9.更改字符串中的部分字符replace
一共有3中替换方法:
1>、replace(char,char),将字符串中与方法中第一个参数相同的字符,统一替换为方法中的第二个参数。
String s1 =new String("hello");
String replaceresult=s1.replace('l','o');
2>、replaceAll(String,String),将字符串中与方法中第一个参数相同的字符串,统一替换成方法中的第二个参数
String s1 =new String("hello");
String replaceresult=s1.replace('ll','LL');
3>、replaceFirst(String,String),将字符串中与方法中的第一个参数字符串相同的第一个字符串替换为方法中的第二个参数字符串
String s1 =new String("hello");
String replaceresult=s1.replace('l','LL');

四、日期和时间类型格式。
在格式化日期时,需要事先导入java.text.*包,该包中包含格式化日期的类函数。

import java.text.*;
public class TestString{
public static void main( String args[]){
SimpleDateFormat formatter = new SimpleDateFormat( "yyyy/M/dd");
java. util. Date date = new java.util. Date();
String sdate = formatter. format(date);
System. out. println(sdate);

}
}

结果是:2017/11/18
注意:格式里面月份要用大写的MM
五、常见面试题及练习
1.字符串字面量(直接给值)是否自动生成一个String对象
是的,JVM在执行双引号操作符的时候,会自动的创建一个String对象,并返回这个对象的引用
2.StringBuffer和StringBuilder存在的作用是什么?
需要大量拼接字符串的话,使用StringBuffer和StringBuilder可以避免不必要的String对象的产生。两者的作用类似,只不过StringBuilder
是线程安全的。
3.如何使用指定的字符集生成String对象?
使用带有字符集编码的String的构造方法。该方法的参数有两个:第一个是byte[]数组,第二个则是字符集编码的字符串形式,如“UTF-8”,“GBK”
import java.text.*;
public class TestString{
public static void main( String args[]){
try{ String a= "中文";
String b= new String( a. getBytes(), "GBK");
String c= new String( a. getBytes(), "UTF-8");
System. out. println(b);
System. out. println(c);
}
catch( Exception e){
e. printStackTrace();
}
}
}
注意: 要处理异常,否则执行报错

练习题
1.分割字符串“oneworldone-dream”划分标志分别是“o”和“n”.

import java.text.*;
public class TestString{
public static void main( String args[]){
String a= "oneworldone-dream";
String [] strbuff = a. split( "o");
for ( int i= 0;i< strbuff. length;i++)
{
String[] strbuff1 = strbuff[i]. split( "n");
for( int j= 0;j< strbuff1. length;j++)
{
System. out. println(strbuff1[j]);
}
}
}
}

2.将字符串“ oneworldone-dream”中的第一个“o”改变为大写,并输出修改后的字符串。
import java.text.*;
public class TestString{
public static void main( String args[]){
String a= "oneworldone-dream";
String b= a. replaceFirst( "o", "O");
System. out. println(b);
}
}
注意:参数要用双引号而不是单引号。

3.创建两个字符串,实现的操作是连接这两个字符串、获取连接后的字符串长度、比较这两个字符串。获取位置length-1的字符。
import java.text.*;
public class TestString{
public static void main( String args[]){
String a= "oneworldone-dream";
String b= new String( "123");
String c= a. concat(b);
System. out. println( c. length());
System. out. println( a. equals(b));
System. out. println( c. charAt( c. length()- 1));
}
}

注意:length()函数的后面不要忘记括号

    

                 

       

 

  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值