黑马程序员—Java基础—String类和Integer、Character类

-----------android培训java培训、java学习型技术博客、期待与您交流!------------  
String类
String类:

java中用String类进行描述。对字符串进行了对象的封装。这样的好处是可以对字符串这种常见数据进行方便的操作。对象封装后,可以定义N多为。

String类概述

字符串是由多个字符组成的一串数据(字符序列)

字符串可以看成是字符数组 

构造方法

public String()                                  无参构造

publicString(byte[] bytes) 把字节数组转成字符串

publicString(byte[] bytes,int offset,int length) 把字节数组的一部分转成字符串

publicString(char[] value) 把字符数组转成字符串

publicString(char[] value,int offset,int count) 把字符数组的一部分转成字符串

publicString(String original) 把字符串常量值转成字符串

特点:字符串一旦被初始化,就不可以被改变,存放在方法区中的常量池中。

String s1 ="abc"; // s1指向的内存中只有一个对象abc。

String s2 = newString("abc"); // s2指向的内容中有两个对象abc、new 。

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

System.out.println(s1.equals(s2));//true,字符串中equals比较的是字符串内容是否相同。

String类的常见操作

String类的判断功能

booleanequals(Object obj) 比较字符串的内容是否相同,区分大小写

booleanequalsIgnoreCase(String str) 比较字符串的内容是否相同,忽略大小写

booleancontains(String str) 判断大字符串中是否包含小字符串

booleanstartsWith(String str) 判断字符串是否以某个指定的字符串开头

booleanendsWith(String str) 判断字符串是否以某个指定的字符串结尾

booleanisEmpty() 判断字符串是否为空。

代码:

  String类的判断功能  46行  Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
          
          
/*
* 注意:
* 字符串内容为空和字符串对象为空。
* String s = "";
* String s = null;
*/
public class StringDemo {
public static void main ( String [] args ) {
// 创建字符串对象
String s1 = "helloworld" ;
String s2 = "helloworld" ;
String s3 = "HelloWorld" ;

// boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
System . out . println ( "equals:" + s1 . equals ( s2 ));
System . out . println ( "equals:" + s1 . equals ( s3 ));
System . out . println ( "-----------------------" );

// boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System . out . println ( "equals:" + s1 . equalsIgnoreCase ( s2 ));
System . out . println ( "equals:" + s1 . equalsIgnoreCase ( s3 ));
System . out . println ( "-----------------------" );

// boolean contains(String str):判断大字符串中是否包含小字符串
System . out . println ( "contains:" + s1 . contains ( "hello" ));
System . out . println ( "contains:" + s1 . contains ( "hw" ));
System . out . println ( "-----------------------" );

// boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
System . out . println ( "startsWith:" + s1 . startsWith ( "h" ));
System . out . println ( "startsWith:" + s1 . startsWith ( "hello" ));
System . out . println ( "startsWith:" + s1 . startsWith ( "world" ));
System . out . println ( "-----------------------" );


// boolean isEmpty():判断字符串是否为空。
System . out . println ( "isEmpty:" + s1 . isEmpty ());

String s4 = "" ;
String s5 = null ;
System . out . println ( "isEmpty:" + s4 . isEmpty ());
// NullPointerException
// s5对象都不存在,所以不能调用方法,空指针异常
System . out . println ( "isEmpty:" + s5 . isEmpty ());
}
}

String类的获取功能

int length() 获取字符串的长度。

char charAt(intindex) 获取指定索引位置的字符

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

intindexOf(String str) 返回指定字符串在此字符串中第一次出现处的索引。

int indexOf(intch,int fromIndex) 返回指定字符在此字符串中从指定位置后第一次出现处的索引。

intindexOf(String str,int fromIndex) 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。

Stringsubstring(int start) 从指定位置开始截取字符串,默认到末尾。

Stringsubstring(int start,int end)  从指定位置开始到指定位置结束截取字符串。

代码:

String类的获取功能  36行  Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
          
          
package cn . itcast_04 ;


public class StringDemo {
public static void main ( String [] args ) {
// 定义一个字符串对象
String s = "helloworld" ;

// int length():获取字符串的长度。
System . out . println ( "s.length:" + s . length ());
// char charAt(int index):获取指定索引位置的字符
System . out . println ( "charAt:" + s . charAt ( 7 ));

// int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
System . out . println ( "indexOf:" + s . indexOf ( 'l' ));

// int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
System . out . println ( "indexOf:" + s . indexOf ( "owo" ));

// int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
System . out . println ( "indexOf:" + s . indexOf ( 'l' , 4 ));
System . out . println ( "indexOf:" + s . indexOf ( 'k' , 4 )); // -1
System . out . println ( "indexOf:" + s . indexOf ( 'l' , 40 )); // -1

// String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引
System . out . println ( "substring:" + s . substring ( 5 ));
System . out . println ( "substring:" + s . substring ( 0 ));
System . out . println ( "----------------------" );

// String substring(int start,int
// end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引
System . out . println ( "substring:" + s . substring ( 3 , 8 ));
System . out . println ( "substring:" + s . substring ( 0 , s . length ()));
}
}

String类的转换功能

byte[] getBytes()  把字符串转换为字节数组。

char[]toCharArray() 把字符串转换为字符数组。

static StringvalueOf(char[] chs) 把字符数组转成字符串。

static StringvalueOf(int i) 把int类型的数据转成字符串。

StringtoLowerCase() 把字符串转成小写。

StringtoUpperCase() 把字符串转成大写。

Stringconcat(String str) 把字符串拼接。

代码:

  String类的转换功能  42行  Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
          
          
public class StringDemo {
public static void main ( String [] args ) {
// 定义一个字符串对象
String s = "JavaSE" ;

// byte[] getBytes():把字符串转换为字节数组。
byte [] bys = s . getBytes ();
for ( int x = 0 ; x < bys . length ; x ++) {
System . out . println ( bys [ x ]);
}

// char[] toCharArray():把字符串转换为字符数组。
char [] chs = s . toCharArray ();
for ( int x = 0 ; x < chs . length ; x ++) {
System . out . println ( chs [ x ]);
}

// static String valueOf(char[] chs):把字符数组转成字符串。
String ss = String . valueOf ( chs );
System . out . println ( ss );

// static String valueOf(int i):把int类型的数据转成字符串。
int i = 100 ;
String sss = String . valueOf ( i );
System . out . println ( sss );

// String toLowerCase():把字符串转成小写。
System . out . println ( "toLowerCase:" + s . toLowerCase ());
System . out . println ( "s:" + s );
// String toUpperCase():把字符串转成大写。
System . out . println ( "toUpperCase:" + s . toUpperCase ());

// String concat(String str):把字符串拼接。
String s1 = "hello" ;
String s2 = "world" ;
String s3 = s1 + s2 ;
String s4 = s1 . concat ( s2 );
System . out . println ( "s3:" + s3 );
System . out . println ( "s4:" + s4 );
}
}

String类的其他功能

替换功能

Stringreplace(char old,char new)

Stringreplace(String old,String new)

去除字符串两空格      

String trim()

按字典顺序比较两个字符串  

intcompareTo(String str)

intcompareToIgnoreCase(String str) 

代码:

  String的其他功能  26行  Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
          
          
public class StringDemo {
public static void main ( String [] args ) {
// 替换功能
String s1 = "helloworld" ;
String s2 = s1 . replace ( 'l' , 'k' );
String s3 = s1 . replace ( "owo" , "ak47" );
System . out . println ( "s1:" + s1 );
System . out . println ( "s2:" + s2 );
System . out . println ( "s3:" + s3 );
// 去除字符串两空格
String s4 = " hello world " ;
String s5 = s4 . trim ();
System . out . println ( "s4:" + s4 + "---" );
System . out . println ( "s5:" + s5 + "---" );

// 按字典顺序比较两个字符串
String s6 = "hello" ;
String s7 = "hello" ;
String s8 = "abc" ;
String s9 = "xyz" ;
System . out . println ( s6 . compareTo ( s7 )); // 0
System . out . println ( s6 . compareTo ( s8 )); // 7
System . out . println ( s6 . compareTo ( s9 )); // -16
}
}
练习:

统计大串中小串出现的次数  woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagn

  String练习  46行  Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
          
          
/*
* 分析:
* 前提:是已经知道了大串和小串。
*
* A:定义一个统计变量,初始化值是0
* B:先在大串中查找一次小串第一次出现的位置
* a:索引是-1,说明不存在了,就返回统计变量
* b:索引不是-1,说明存在,统计变量++
* C:把刚才的索引+小串的长度作为开始位置截取上一次的大串,返回一个新的字符串,并把该字符串的值重新赋值给大串
* D:回到B
*/
public class StringTest4 {
public static void main ( String [] args ) {
// 定义大串
String maxString = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun" ;
// 定义小串
String minString = "java" ;

// 写功能实现
int count = getCount ( maxString , minString );
System . out . println ( "Java在大串中出现了:" + count + "次" );
}

/*
* 两个明确: 返回值类型:int 参数列表:两个字符串
*/
public static int getCount ( String maxString , String minString ) {
// 定义一个统计变量,初始化值是0
int count = 0 ;

// 先在大串中查找一次小串第一次出现的位置
int index = maxString . indexOf ( minString );

// 索引不是-1,说明存在,统计变量++
while ( index != - 1 ) {
count ++;
// 把刚才的索引+小串的长度作为开始位置截取上一次的大串,返回一个新的字符串,并把该字符串的值重新赋值给大串
int startIndex = index + minString . length ();
maxString = maxString . substring ( startIndex );
// 继续查
index = maxString . indexOf ( minString );
}

return count ;
}
}

StringBuffer类概述及其构造方法

StringBuffer类概述

我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题

线程安全的可变字符序列

构造方法

publicStringBuffer() 无参构造方法  

publicStringBuffer(int capacity) 指定容量的字符串缓冲区对象

publicStringBuffer(String str) 指定字符串内容的字符串缓冲区对象

添加功能

publicStringBuffer append(String str)

publicStringBuffer insert(int offset,String str)

删除功能

publicStringBuffer deleteCharAt(int index)

publicStringBuffer delete(int start,int end)

替换功能

publicStringBuffer replace(int start,int end,String str)

反转功能      

 public StringBuffer reverse()

截取功能

public String substring(intstart)

public Stringsubstring(int start,int end)

String、StringBuffer、StringBuilder的区别?

答:A:String是内容不可变的,而StringBuffer,StringBuilder都是内容可变的。

B:StringBuffer是同步的,数据安全,效率低;StringBuilder是不同步的,数据不安全,效率高

基本数据类型包装类

基本数据类型对象包装类:是按照面向对象思想将基本数据类型封装成了对象。

好处:

1:可以通过对象中的属性和行为操作基本数据。

2:可以实现基本数据类型和字符串之间的转换。

关键字   对应的类名

byte     Byte

short    Short    

int   Integer   

long     Long

float    Float

double    Double

char     Character

Boolean   Boolean

基本数据类型对象包装类:都有 XXXparseXXX 方法

只有一个类型没有parse方法:Character ;

Integer类

数字格式的字符串转成基本数据类型的方法:

1:将该字符串封装成了Integer对象,并调用对象的方法intValue();

2:使用Integer.parseInt(numstring)—>类.方法名:不用建立对象,直接类名调用;

 

将基本类型转成字符串:

1:Integer中的静态方法 String toString(int);

2:int+"";

 

将一个十进制整数转成其他进制:

        转成二进制:toBinaryString

        转成八进制:toOctalString

        转成十六进制:toHexString

        toString(int num,int radix);

 

将其他进制转换十进制:

parseInt(string,radix);//将给定的数转成指定的基数进制;

 

在jdk1.5版本后,对基本数据类型对象包装类进行升级。在升级中,使用基本数据类型对象包装类可以像使用基本数据类型一样,进行运算。

        Integer i = new Integer(4); //1.5版本之前的写法;

        Integer i = 4; //自动装箱,1.5版本后的写法;

        i = i + 5;

        //i对象是不能直接和5相加的,其实底层先将i转成int类型,在和5相加。而转成int类型的操作是隐式的。自动拆箱:拆箱的原理就是i.intValue();i+5运算完是一个int整数。如何赋值给引用类型i呢?其实有对结果进行装箱。

Integer c = 127;

        Integer d = 127;

        System.out.println(c = = d); //true

        //在装箱时,如果数值在byte范围之内,那么数值相同,不会产生新的对象,也就是说多个数值相同的引用指向的是同一个对象。

代码:

  Integer自动拆装箱  27行  Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
          
          
/*
* JDK5的新特性
* 自动装箱:把基本类型转换为包装类类型
* 自动拆箱:把包装类类型转换为基本类型
*
*/
public class IntegerDemo {
public static void main ( String [] args ) {
// 定义了一个int类型的包装类类型变量i
// Integer i = new Integer(100);
Integer ii = 100 ;
ii += 200 ;
System . out . println ( "ii:" + ii );

// 通过反编译后的代码
// Integer ii = Integer.valueOf(100); //自动装箱
// ii = Integer.valueOf(ii.intValue() + 200); //自动拆箱,再自动装箱
// System.out.println((new StringBuilder("ii:")).append(ii).toString());

Integer iii = null ;
// NullPointerException
if ( iii != null ) {
iii += 1000 ;
System . out . println ( iii );
}
}
}

注意一个小问题:

在使用时,Integer  x = null;因为Integer  x=null;x在这里也是一个对象,所以应该事先判断该对象是否存在。否则代码就可能会出现NullPointerException。建议先判断是否为null,然后再使用。

Character类概述及其构造方法

Character类概述

Character 类在对象中包装一个基本类型 char 的值

此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然

构造方法

publicCharacter(char value)

Character类成员方法

public staticboolean isUpperCase(char ch) 判断给定的字符是否是大写字符

public staticboolean isLowerCase(char ch) 判断给定的字符是否是小写字符

public staticboolean isDigit(char ch) 判断给定的字符是否是数字字符

public staticchar toUpperCase(char ch) 把给定的字符转换为大写字符

public staticchar toLowerCase(char ch)  把给定的字符转换为小写字符

代码:

  Chracter的基本功能 26行  Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
             
             
public class CharacterDemo {
public static void main ( String [] args ) {
// public static boolean isUpperCase(char ch):判断给定的字符是否是大写字符
System . out . println ( "isUpperCase:" + Character . isUpperCase ( 'A' ));
System . out . println ( "isUpperCase:" + Character . isUpperCase ( 'a' ));
System . out . println ( "isUpperCase:" + Character . isUpperCase ( '0' ));
System . out . println ( "-----------------------------------------" );
// public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符
System . out . println ( "isLowerCase:" + Character . isLowerCase ( 'A' ));
System . out . println ( "isLowerCase:" + Character . isLowerCase ( 'a' ));
System . out . println ( "isLowerCase:" + Character . isLowerCase ( '0' ));
System . out . println ( "-----------------------------------------" );
// public static boolean isDigit(char ch):判断给定的字符是否是数字字符
System . out . println ( "isDigit:" + Character . isDigit ( 'A' ));
System . out . println ( "isDigit:" + Character . isDigit ( 'a' ));
System . out . println ( "isDigit:" + Character . isDigit ( '0' ));
System . out . println ( "-----------------------------------------" );
// public static char toUpperCase(char ch):把给定的字符转换为大写字符
System . out . println ( "toUpperCase:" + Character . toUpperCase ( 'A' ));
System . out . println ( "toUpperCase:" + Character . toUpperCase ( 'a' ));
System . out . println ( "-----------------------------------------" );
// public static char toLowerCase(char ch):把给定的字符转换为小写字符
System . out . println ( "toLowerCase:" + Character . toLowerCase ( 'A' ));
System . out . println ( "toLowerCase:" + Character . toLowerCase ( 'a' ));
}
}
练习: 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。 ( 不考虑其他字符 )

分析:

A:定义三个统计变量。

int big=0;

 int smal=0;

 int number=0;

B:键盘录入一个字符串。

 C:把字符串转换为字符数组。

D:遍历字符数组获取到每一个字符

E:判断该字符是

大写     big++;

小写     smal++;

数字     number++;

 F:输出结果即可

  练习  28行  Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
             
             
public class CharacterTest {
public static void main ( String [] args ) {
int big = 0 ;
int small = 0 ;
int number = 0 ;

Scanner sc = new Scanner ( System . in );
System . out . println ( "请输入一个字符串:" );
String line = sc . nextLine ();

char [] chs = line . toCharArray ();

for ( int x = 0 ; x < chs . length ; x ++) {
char ch = chs [ x ];

if ( Character . isUpperCase ( ch )) {
big ++;
} else if ( Character . isLowerCase ( ch )) {
small ++;
} else if ( Character . isDigit ( ch )) {
number ++;
}
}
System . out . println ( "大写字母:" + big + "个" );
System . out . println ( "小写字母:" + small + "个" );
System . out . println ( "数字字符:" + number + "个" );
}
}
-----------android培训java培训、java学习型技术博客、期待与您交流!------------ 




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值