Java:String类

一、String类

  1. Java专门提供了用来处理字符序列的String类,其放在Java.lang包

  2. String 类被声明为final 类, 所以不能扩展String 类,且不可以有子类

  3. 字符常量:“你好”、“12.7”、“boy”,也是字符对象;

  4. 字符串变量:String s =new String("we are family ");
    (1) 也可以不调用构造函数直接:String s = “we are family”;
    (2)可以用字符串对象:String tom = new String(s);

  5. String常用两个构造方法

字符串的深入原理分析

这个地址拉到下面就有详细的解释
在这里插入图片描述
(1) **String(char a[ ])**用一个字符数组创建一个字符串对象,如:

char a[]={'j','a'.'v','a'};
String s = new String(a);
相当于
String s =new String ("java");

(2)String(char a[ ] , int startIndex , int count);
提取字符数组a中的一部分字符创建一个字符串对象
startIndex 即起始位置, count 即提取多少个

char a[]={'一','二','三','四','五'};
String s =new String(a,2,3);
相当于
String s =new String("三四五");
  1. 字符串常量是对象,所以可以吧字符串直接赋值给字符串变量:String s1 =" hello ; String s2=" hello ";
    注意此时s1 和 s2 存放的地址是一样的,即一个hello
    跟上面区分开来的是:
    String s1 =new String(“hello”);
    String s2= new String(“hello”);
    此时的s1 和s2 De 地址是不一样的,即两个hello
  2. public int length() 方法可以获取字符串长度
String s="hello java";
int n1,n2;
n1="java".length();
n2=s.length();
  1. public boolean equals(String s) 判断字符串 s 是否与调用该方法的对象一样,一样返回 ture 反之 false
  2. public boolean equalsIgnoreCase(String s) 忽略大小写
  3. public boolean startWith(String s) 判断对象前缀是否与 字符串 s 一样
  4. public boolean endsWith(String s) 判断对象后缀是否与 字符串 s 一样
  5. public intcompareTo(String s) 按字典大小 判断与字符串大小关系,大于s则返回正值,小于返回负值,等于返回0;
    public intcompareToIgnoreCase(String s) 忽略大小写
String str="abcde";
str.compareTo("boy")//返回负 a<b
str.compareTo("aba")//返回正 c>a
  1. public boolean contains(String s) 字符串对象调用contain方法,判断当前字符串对象是否含有参数指定的字符串s,例:“”student”.contains(“stu”) 结果是返回ture的

  2. public int indexOf(String s) 从对象的头部开始检索是否出现字符串s,有返回第一出现的下标位置

  3. public int indexOf(String s,int startpoint) 从对象的下标位statpoint处开始检索

  4. public int lastIndexOf(String s) 返回最后一次出现的下标,没有检索到返回 -1

String tom="I am a good cat";
tom.indexOf("a");     // 返回2
tom.indexOf("good",2);//返回7
tom.indexOf("a",7);  // 返回13
tom.indexOf("w",2);  // 返回-1
  1. public String substring (int startpoint) 当前对象字符串从下标位startpoint 处开始到末尾截取这段字符串成为新的字符串对象;
  2. public String substring(int start ,int end) 截取到end,但不包含end下标本身,包含start下标本身
  3. public String trim() 去掉当前字符串对象的前后空格,生成新的字符串对象
public class Example{
public static void main(String args[]){
String path="c:\\book\\javabook\\java programmer.doc";
int index = path.indexOf("\\");
index=path.indexOf("\\",index);
String sub= path.substring(index);
System.out.printlin(sub);
index=path.lastIndexOf("\\");
sub=path.substring(index+1);
System.out.println(sub);
System.out.println(sub.contains("programmer"));
   }
}
  1. 字符串与基本数据的互相转化:在java的lang包中的类 Integer 可以调用自身类方法:public static int parseInt(String s)
int x;
String s = "876";
x=Integer.parsenInt(s);

类似的,使用java.lang 保重的Byte 、Short 、 Long 、Float 、Double

  1. 数字向字符串转换:public static String valueOf( int n) 如:String str = String.valueOf (12313.9876)
  2. main方法中的 args 参数能接收用户从键盘输入的字符串,args【0】,args【1】等下标表示各字符串
public class Example{
public static void main(args[])
{ 
double aver=0,sum=0,item=0;
boolean computable =ture;
for(String s=args){
 try{item=Double.paresDouble(s);
    sum=sum+item;
    catch(NumberFormatExcaption e){
    System.out.println("您键入刻非数字字符:"+e);
    computable=false;
    }}
if(computable)
System.out,prinln("sum="+sum);
}
}

  1. 所有的类都默认是 java.lang包中Object类的子类或间接类

  2. 字符串放到数组中的方法:public void getChars( int start, int end ,char c[ ] , int offset),当前对象将本身字符串存放到指定的位置:c 数组中
    从start ~ end-1位置上的字符串复制
    从c中的offset下标开始存放

  3. public char[ ] toCharArray()字符串对象调用该方法返回一个字符数组,长度和字符串长度相等

public class Example{
public static void main(args[])
{ char [],a,b,c;
String s = "20019年10月1日是国庆70周年";
a=new char[2];
s.getChars(11,13,a,0); //s放到a 中
System.out,println(a);
c="十一长假期间,学校都放假".toCharArray();//字符串对象调用自身函数
for(char ch:c)
System.out.print(ch);
}}

字符串与字节数组,字符串的加密算法
正则表达式

二、StringBuffer类

  1. 该类可以创建字符串序列,该类对象的实体空间是可以自动改变大小,不像String 类是无法改的, 便于存放一个可变的字符序列

  2. 三个构造方法:
    StringBuffer():创建一个对象分配16个字符大小容量,当存放长度大于16时自动增加
    StringBuffer(int size ):可以指定size 个字符容量,大于时自动加
    StringBuffer(String s):可以指定空间 s长度+16个字符,也自动加
    可以用:length()知道长度,capacity()获取实际容量

  3. append(String s) 方法可以对该类对象字符串进行追加

StringBuffer s = "我喜欢";
s.append("打篮球")

StringBuffer append(String s ) 将字符串对象追加到当前对象中,并返回当前变化后的对象引用
StringBuffer append(int n ) 将一个int 型数据转化为字符串对象后再追加到当前对象,并返回变化后的对象
StringBuffer append(Object o) 将一个Object 对象字符串表示追加到当前对象中,并返回当前对象的引用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值