JAVA String类的基本用法

```JAVA String类的基本用法
1、String对象的初始化
由于String对象特别常用,所以在对String对象进行初始化时,Java提供了一种简化的特殊语法,格式如下:
String s = “abc”;
s = “Java语言”;
其实按照面向对象的标准语法,其格式应该为:
String s = new String(“abc”);
s = new String(“Java语言”);
只是按照面向对象的标准语法,在内存使用上存在比较大的浪费。例如String s = new String(“abc”);实际上创建了两个String对象,一个是”abc”对象,存储在常量空间中,一个是使用new关键字为对象s申请的空间。
其它的构造方法的参数,可以参看String类的API文档。

2.取字符串charAt(int index)

package com.study;public class Test {	
public static void testString(){		
<strong>String str=new String("Hello Boy");	
	char c=str.charAt(0); //取字符串操作</strong>		
	System.out.print(c);	
	}			
	public static void main(String[] args) {		
	   Test.testString();	
	}

输出:H
-比较字符串compareTo(Object o)、
compareTo(String str)、
CompareToIgnore(String str)

package com.study;
public class Test {	public static void testString(){ <strong>//compareTo()和CompareToIgnore()返回值为0,则表示相同</strong>		
String str=new String("Hello Boy");		
String str2 ="Hello Boy";		
<strong>System.out.println("compareTo:"+(str.compareTo(str2)==0));	
	System.out.println("compareToIgnoreCase:"+(str.compareToIgnoreCase(str2)==0));</strong>	
}	
	public static void main(String[] args) {		
	Test.testString();
	<pre name="code" class="java">String s="you are a little boy!";
	char[] c=new char[s.length()];
	c=s.toCharArray();
	System.out.print(c);
}}

输出结果:
compareTo:truecompareToIgnoreCase:true

连接字符串concat(String str)

String str=new String("01234");
String str2 ="5678";
System.out.println(str.concat(str2));

输出结果:012345678
字符数组转为字符串 copyValueOf(char[] data) 、 copyValueOf(char[] data,int offset,int count) ,
String(char[] data)、
String(char[] ,int,int)

char data[]={'a','b','c','d'};
String str=String.copyValueOf(data);		
System.out.println(str);	

输出结果:abcd
字符串转为字符数组getChars(int beigin,int end,char[] data,int dstBegin)、toCharArray

String s="you are a little boy!";
char[] c=new char[s.length()];
s.getChars(0, s.length(), c, 0);
System.out.print(c);

输出结果:you are a little boy!

  String s="you are a little boy!";
char[] c=new char[s.length()];
c=s.toCharArray();
System.out.print(c);

输出结果:you are a little boy!*
判断字符串结束内容endsWith(String s)
同理:判断起始子串startsWith(String s),startsWith(String s,int toffset)

String s="you are a little boy!";
boolean b=s.endsWith("boy!");
System.out.print(b);

输出结果:true
字符串对象是否相等equals(Object o)、equalsIgnoreCase(String s)
****

String s="you are a little boy!";
boolean b1=s.equals("boy!");
boolean b2=s.equals("you are a little boy!");
boolean b3=s.equalsIgnoreCase("you are a little boy!");
System.out.print(b1+","+b2+","+b3);

输出结果:false,true,true
字符串中的字符的定位indexOf(int ch)、indexOf(int ch,int fromindex)、indexof(String s),indexOf(String s,int fromindex)

String s="you are a little boy!";
int pos =s.indexOf('a');
int pos1=s.indexOf("are");
int pos2=s.indexOf('y',2);
System.out.print(pos+"和"+pos1+"and"+pos2);

输出结果:4和4and19
字符串中的字符最后的位置lastIndexOf(int ch),lastIndexOf(int ch,int fromindex),lastIndexOf(String s),lastIndexOf(String s,int fromindex)

String s="you are a little boy!";
int pos =s.lastIndexOf('a');
int pos1=s.lastIndexOf("are");
inpos2=s.lastIndexOf('y',2);
System.out.print(pos+"和"+pos1+"and"+pos2);

输出结果:8和4and0
判断长度length()
int Len=(new Stirng(“abc”)).length();

*字符串内容的替换replace()

*String s="you are a little boy!";
s.replace('b', 'B');
System.out.println(s);
System.out.println(s.replace('b', 'B'));

*输出结果:
you are a little boy!
you are a little Boy!

取子字符串subString(int begin),subString(int begin,int end)

String s="you are a little boy!";	
System.out.println(s.substring(0));
System.out.println(s.substring(0,5));*

输出结果:
you are a little boy!

字符串大小写转换toUpperCase().toLowerCase()
**

String s="you are a little boy!";	
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());

**
输出结果:
YOU ARE A LITTLE BOY!
you are a little boy!

将其他类型转为String:valueOf()

int n=5;
char[] c={'a','b'};
String s1,s2;	
s1=String.valueOf(n);
s2=String.valueOf(c);
System.out.print(s1+"和"+s2);

输出结果:5和ab

用双引号括起来的字符串都是String类的一个实例。
String e = “”; \ 空字符串
String greeting = “Hello”;
子串
使用substring方法可以提取子串。例如:
String greeting = “hello”;
String s = greeting.substring(0,3); \ s等于“Hel”
字符串拼接
Java语言允许使用+号连接两个字符串:
String expletive = “Expletive”;
String PG13 = “deleted”;
String message = expletive + PG13;
message等于“Expletivedeleted”
当一个字符串与非字符串的值拼接时,后者会被转换成字符串。例如:
int age = 13;
String rating = “PG” + 13; \ rating等于 “PG13”
这种特性经常在输出语句中使用
字符串是不可变的
String类没有提供修改字符串的方法,字符串中的字符是不能修改的,例如:“Hello”字符串包含的字符永远是H,e,l,l,o
但是可以修改字符串变量,例如:
greeting = greeting.substring(0,3) +”p!”;
或者 greeting = “Help!”;
空串和null串
空串””是长度为0的字符串,判断空串的语句:
if( str.length() == 0) 或 if(str.equals(“”))
空串是一个Java对象,null串表示没有任何对象与该变量关联,声明一个字符串变量但是不初始化,则这就是个null串,判断null串的语句:
if(str == null)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值