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

在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);
    }
}

输出结果:abc

public 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 welcome

String 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如何遍历字符串:

String s="abcde";
	    for(int i=0;i<s.length();i++)
        {
	    		char c=s.charAt(i);
	    		System.out.print(c+" ");//输出a b c d e,获取字符串
        }

	    String[] s1={"a","b","c","d","e"};
	    for(int i=0;i<s1.length;i++)
	    {
 System.out.print(s1[i]+" ");//输出a b c d e,获取字符串数组
	    }

Java如何把String字符串转换为数字?

1、转换为浮点型:   

使用Double或者Float的parseDouble或者parseFloat方法进行转换

String   s   =   "123.456 ";  
double   d   =   Double.parseDouble(s); 
float   f   =   Float.parseFloat(s);

转换为整型: 

使用Integer的parseInt方法进行转换。

int i = Integer.parseInt(str);//str待转换的字符串

Java如何实现位数对齐(方法不止一种,此处仅介绍我自己用习惯的那一种大笑):

	    System.out.println(String.format("%02d:%02d:%02d",hh,mm,ss));

还有,Java字符串中还有一个非常非常好用的函数,力推啊!帮忙解决了很多问题,真的很好用(emmmm,我咋突然感觉自己像买东西滴(此处手动滑稽~~~))

让我告诉你它金光闪闪的名字:IndexOf()!!!

因为它今天下午帮了我很大很大的忙,所以我很感谢它,为它疯狂打call!

emmmm,废话不多说,回到正题。

举个例子吧!

package test_str;

import java.util.*;

public class Str {

    public static void main(String[] args) {
        String str="Hello, world, !";
        int n1 = str.indexOf(',');
        System.out.println(n1);
        int n2 = str.indexOf(',',n1+1);
        System.out.println(n2);
        int n3 = str.indexOf(',',n1);
        System.out.println(n3);
        System.out.println(str.startsWith("H"));
        System.out.println(str.endsWith("!"));

    }

}

运算输出结果:

5
12
5
true
true

str.indexOf(','),表示在整个str字符串中检索!

int n3 = str.indexOf(',',n1);表示从n1开始检索!

哈哈哈,最后再补充一个小知识: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

注意:Java里面分离小数点要用到转义字符,否则得不到正确答案!

String []s = str.split("\\.");

哈哈哈,我又来了!中午还说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

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


Java对字符串进行与运算:

String str1="0011";
String str2 = "1110";
System.out.println(Integer.parseInt(str1)&Integer.parseInt(str2));

输出结果:

2
字符串与运算要先转化成数字,然后按照数字的与运算规则进行与运算!












  

 

  

  


  • 38
    点赞
  • 131
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值