您的位置:首页 > 编程语言 > Java开发

Java中对字符串的一些常见处理

2018-04-03 15:00 417 查看
在Java中,处理字符串、文本的时候,一般常用一下三种类:
String、StringBuffer、StringBuilder
三者分别有各自适用的场合。
String:适用于少量的字符串操作的情况。
StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况。
StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况。

在运行方面速度快慢为:StringBuilder > StringBuffer > String
String最慢的原因:
 String为字符串常量,而StringBuilder和StringBuffer均为字符串变量,即String对象一旦创建之后该对象是不可更改的,但后两者的对象是变量,是可以更改的。
对于Java初学者来说,初步掌握string是必不可少的。而StringBuffer和StringBuilder我们先不必掌握,稍有了解即可。以后有应用的场合我们再作补充。
String用法:
1、将数组中元素以字符串形式输出
数组可以使byte类型的,也可以是char类型的(强制转换为字符串)。举例如下:public class str
{
public static void main(String[] args)
{
byte[] b={97,98,99};
String str=new String(b);
System.out.println(str);
}
}输出结果:abcpublic class strTest
{
public static void main(String[] args)
{
char[] c={'H','e','l','l','o'};
String str=new String(c);
System.out.println(str);
}
}输出结果:Hello
当然,也可以把数组中的特定元素片段转换成字符串输出。public class strTest
{
public static void main(String[] args)
{
byte[] b={97,98,99,100,101,102};
String str=new String(b,3,2);
System.out.println(str);
}
}输出结果:de
从数组元素b[3]开始(包括b[3]在内)的连续两个元素以字符串形式输出。

2、字符串中的一些常用函数:连接concat()、提取substring()、charAt()、length()、equals()、equalsIgnoreCase()等等。String str1="you";
String str2=" welcome";
System.out.println(str1.concat(str2));输出结果:you welcomeString str="we are students and he is a techer";
System.out.println(str.substring(2,10));输出结果: are stu

从str[2]开始,到str[9]结束。
substring() 方法用于提取字符串中介于两个指定下标之间的字符。

stringObject.substring(start,stop) String str="we are students and he is a worker";
System.out.println(str.charAt(1));输出结果:e

charAt(int index)方法是一个能够用来检索特定索引下的字符的String实例的方法.charAt()方法返回指定索引位置的char值。索引范围为0~length()-1.如: str.charAt(0)检索str中的第一个字符,str.charAt(str.length()-1)检索最后一个字符.
String str="we are";
System.out.println(str.length());
输出结果:6
比较两个字符串是否相同用equals()public class test {

public static void main(String[] args) {
String str1="we are students and he is a worker";
String str2="we are students and he is a worker";
System.out.println(str1.equals(str2));
}
}输出结果:true
忽略字符大小写的字符串比较用equalsIgnoreCase()public class test {

public static void main(String[] args) {
String str1="we are students and he is a worker";
String str2="We ARE students AND he is a worker";
System.out.println(str1.equalsIgnoreCase(str2));
}
}
输出结果:true
3、字符串的一些检索查找字符的函数public class test {

public static void main(String[] args) {
String str="我们一起数到6吧!";
System.out.println(str.indexOf("一"));
         System.out.println(str.indexOf("6"));
        System.out.println(str.startsWith("我"));
        System.out.println(str.endsWith("!"));
}
}
输出结果:
2

6
true
true



哈哈哈,最后再补充一个小知识:Java的split函数的基本用法 public static void main(String[] args) {
              String str="good good study, day day up";
String[] strarray=str.split(" ");
for (int i = 0; i < strarray.length; i++)
System.out.println(strarray[i]);
}输出结果:
good
good
study,
day
day
up



哈哈哈,我又来了!中午还说StringBuffer目前用不到呢,晚上就打脸了。。。。。。
下面来总结一下StringBuffer的一些常见用法:
StringBuffer支持以下几种操作函数:append()、insert()、replace()、delete()、reserve()等等。

public StringBuffer append(String s):将指定的字符串追加到此字符序列。
StringBuffer insert(int index,String str):在index前插入字符串。

replace(int start, int end, String str):使用给定 
String
 中的字符替换此序列的子字符串中的字符。

public delete(int start, int end):移除此序列的子字符串中的字符。(从start开始,包含start,到end结束,不包含end)

public StringBuffer reverse():将此字符序列用其反转形式取代。public class Str {

public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer("good");
sb.append(" study");
System.out.println(sb);
sb.reverse();
System.out.println(sb);
sb.delete(1,3);
System.out.println(sb);
sb.insert(3, "hello");
System.out.println(sb);
sb.insert(0, "11");
System.out.println(sb);
sb.replace(1, 2, "hello");
System.out.println(sb);
}

}输出结果见下:
good study
yduts doog
yts doog
ytshello doog
11ytshello doog
1helloytshello doog

总之,记住了一些常用方法,其实还是很简单的啦!


  

 

  
  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: