黑马程序员--String

----------------- android培训java培训、期待与您交流!-----------------


String类
在java中,String字符串是个非常强大且常用的类。掌握字符串的使用,是我们学习java的基础。


1   字符串的创建

字符串的创建非常简单
String  s =“java”;   这是字符串创建最简单的方式,也非常的方便。
还有种正规的创建法
String  s  =  new  String("java");
还可以重新创建  s  =  new String("aaa zhy");
只是按照面向对象的标准语法,在内存使用上存在比较大的浪费。
例如String s = new String(“java”);实际上创建了两个String对象,一个是”java”对象,存储在常量空间中,一个是使用new关键字为对象s申请的空间。


2、字符串的常见操作


1  charAt()
该方法的作用是按照索引值,获得字符串中的指定字符。例如:
String s = "hellow   java   ";
System.out.println(s.charAt(1));   
返回的结果是第2个字符e


       2、equals()

该方法的作用是判断两个字符串对象的内容是否相同。如果相同则返回true,否则返回false。例如:
String s = “abc”;
String s1 = new String(“abc”);
boolean b = s.equals(s1);

注意:==  和  equals()的区别
==  比较的是对象是否相同,包括内容和存储地址。
而equals()比较的仅仅是对象的内容

      3、endsWith()

该方法的作用是判断字符串是否以某个字符串结尾,如果以对应的字符串结尾,则返回true。
String s = “abc.java”;
boolean bl = s.endsWith(“.java”);
则变量bl的值是true。

4、startsWith()

该方法的作用和endsWith方法类似,只是该方法是判断字符串是否以某个字符串作为开始。例如:
String s = “TestGame”;
boolean b = s.startsWith(“Test”);   //结果是true

5、compareTo()

字符串的默认比较方式,比较2个字符串的大小,根据字符串的编码大小比较。返回正数,负数,0分别对应 着大于,小于,等于。
String s = “abc”;
String s1 = “abcd”;
int value = s.compareTo(s1);  则value的值是小于0的值,即-1。
在String类中还存在一个类似的方法compareToIgnoreCase,这个方法是忽略字符的大小写进行比较,比较的规则和compareTo一样。


    6、getBytes()

该方法的作用是将字符串转换为对应的byte数组,从而便于数据的存储和传输。例如:
String s = "hellow   java   ";
byte [] buf = s.getBytes();
byte [] bt = s.getBytes("happy ");

    7、indexOf()

该方法的作用是查找特定字符或字符串在当前字符串中的起始位置,如果不存在则返回-1。例如:
String s = “zhykjhkjsakdj”;
int a1 = s.indexOf(‘h’);    //结果是1
int a2 = s.indexOf(‘Q’);  //结果是-1

    8、length()

该方法的作用是返回字符串的长度,也就是返回字符串中字符的个数。中文字符也是一个字符。例如:
String s = “abc”;
String s1 = “Java语言”;
int len = s.length();
int len1 = s1.length();
则变量len的值是3,变量len1的值是6.         
    
    9、split()

该方法的作用是以特定的字符串作为间隔,拆分当前字符串的内容,一般拆分以后会获得一个字符串数组。 例如:
String s = “aa,bb,cc,dd”;
String s1[] = s.split(“,”);
代码返回的字符串数组的元素是   {aa,bb,cc,dd}
该方法是解析字符串的基础方法。
果字符串中在内部存在和间隔字符串相同的内容时将拆除空字符串,尾部的空字符串会被忽略掉。例如:
String s = "123221412";
String s1[] = s.split("2");
for (String ss: s1) {
System.out.println(ss);  
}
打印出的结果是  1,3,   ,141
如果需要限定拆分以后的字符串数量,则可以使用另外一个split方法,例如:
String s = "123221412";
String s1[] = s.split("2",2);
for (String ss: s1) {
System.out.println(ss);  
}


s.split("2",2);打印出的结果是  1,3221412。这方法就是将字符串最多拆分x次 

    10、substring()

该方法的作用是取字符串中的“子串”,所谓“子串”即字符串中的一部分。例如“23”是字符串“123”的子串。
字符串“123”的子串一共有6个:”1”、”2”、”3”、”12”、”23”、”123”。而”32”不是字符串”123”的子串。
例如:
String s = “Test”;
String s1 = s.substring(2);
则该代码的作用是取字符串s中索引值为2(包括)以后的所有字符作为子串,则字符串s1的值是”st”。
如果数字的值和字符串的长度相同,则返回空字符串。例如:
String s = “Test”;
String s1 = s.substring(4);
则字符串s1的值是””。
如果需要取字符串内部的一部分,则可以使用带2个参数的substring方法,例如:
String s = “TestString”;
String s1 = s.substring(2,5);
则该代码的作用是取字符串s中从索引值2(包括)开始,到索引值5(不包括)的部分作为子串,则字符串s1的值是”yasd”。
下面是一个简单的应用代码,该代码的作用是输出任意一个字符串的所有子串。代码如下:


11、trim()
该方法的作用是去掉字符串开始和结尾的所有空格,然后形成一个新的字符串。该方法不去掉字符串中间的 空格。例如:
String s = " zhy fasjkda dd "; 
System.out.println(s.trim());
返回的结果是zhy fasjkda dd。


下面看看具体代码做个一些小例子
package com.zhy;


public class StringDemo {


<span style="white-space:pre">	</span>public static void main(String[] args) {
<span style="white-space:pre">		</span>String s = "   happy every day !  ";
<span style="white-space:pre">		</span>String s1= "!!--  zhy@ --*-*--";
<span style="white-space:pre">		</span>System.out.println("原字符串:"+s+"结束");
<span style="white-space:pre">		</span>System.out.println(trim(s));
<span style="white-space:pre">		</span>System.out.println(s1);
<span style="white-space:pre">		</span>System.out.println(reverseString(s1));
<span style="white-space:pre">		</span>System.out.println();
<span style="white-space:pre">		</span>String s2="abc"; String s3= "abcksglabkabcdekk--abcd";
<span style="white-space:pre">		</span>System.out.println("字符串"+s2+"在字符串"+s3+"中出现了"+getstoscount(s2, s3)+"次");
<span style="white-space:pre">		</span>String s4="hellowasd",s5="ksadkhellowglsdjkhellow1";
<span style="white-space:pre">		</span>System.out.println("字符串"+s4+"和字符串"+s5+"的最长子字符串是"+getMaxString(s4, s5));
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 模拟字符串trim()方法--实现去除字符串2端空格
<span style="white-space:pre">	</span> * @param s   待处理的字符串
<span style="white-space:pre">	</span> * @return     新的字符串
<span style="white-space:pre">	</span> * startsWith(s)--检测字符串是否以s字符开始
<span style="white-space:pre">	</span> * endsWith(s)  --检测字符串是否以s字符结束
<span style="white-space:pre">	</span> * substring(int x,int y )-- 从x的位置开始到y位置结束获取新的子字符串
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>private static String trim(String s){
<span style="white-space:pre">		</span>while (s.startsWith(" ") || s.endsWith(" ")) {
<span style="white-space:pre">			</span>if (s.startsWith(" ")) {
<span style="white-space:pre">				</span>s = s.substring(1);
<span style="white-space:pre">			</span>}else{
<span style="white-space:pre">				</span>s = s.substring(0, s.length()-1);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>/*  
<span style="white-space:pre">		</span> * 方法二
<span style="white-space:pre">		</span>int start =0,end=s.length()-1;
<span style="white-space:pre">		</span>while(s.charAt(start)==" " &&  start<end)
<span style="white-space:pre">			</span>start ++;
<span style="white-space:pre">		</span>while(s.charAt(end)==" " && start<end)
<span style="white-space:pre">			</span>end--;
<span style="white-space:pre">		</span>return s.substring(start,end);
<span style="white-space:pre">		</span>*/
<span style="white-space:pre">		</span>return s;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 字符串反转
<span style="white-space:pre">	</span> * @param s   待处理的字符串
<span style="white-space:pre">	</span> * @param start   反转的开始位置
<span style="white-space:pre">	</span> * @param end     反转的结束位置
<span style="white-space:pre">	</span> * @return   新的字符串
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>private static String reverseString(String s,int start,int end){
<span style="white-space:pre">		</span>char  [] num  = s.toCharArray();;
<span style="white-space:pre">		</span>reverse(num,start,end);
<span style="white-space:pre">		</span>return new String(num);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 字符串反转
<span style="white-space:pre">	</span> * @param s   待处理的字符串
<span style="white-space:pre">	</span> * @return     新的字符串
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>private static String reverseString(String s){
<span style="white-space:pre">		</span>return reverseString(s, 0, s.length());
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 获取一个字符串s在另一个字符串s1中出现的个数
<span style="white-space:pre">	</span> * @param s    短字符串s
<span style="white-space:pre">	</span> * @param s1  长字符串s1
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>private static int getstoscount(String s,String s1){
<span style="white-space:pre">		</span>int count = 0,index=0;
<span style="white-space:pre">		</span>while ( (index = s1.indexOf(s)) != -1) {
<span style="white-space:pre">			</span>s1 = s1.substring(index+s.length());
<span style="white-space:pre">			</span>count ++;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return count;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 获取2个字符串中的最长子字符串
<span style="white-space:pre">	</span> * @param s1
<span style="white-space:pre">	</span> * @param s2
<span style="white-space:pre">	</span> * @return
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>private static String getMaxString(String s1,String s2){
<span style="white-space:pre">		</span>String max="",min="";
<span style="white-space:pre">		</span>max = (s1.length()>s2.length())? s1:s2;
<span style="white-space:pre">		</span>min  = (s1.length()<s2.length())? s1:s2;
<span style="white-space:pre">		</span>for (int i = 0; i < min.length(); i++) {
<span style="white-space:pre">			</span>for (int j=0,z=min.length()-i;  z!=min.length()+1; j++,z++) {
<span style="white-space:pre">				</span>String tem =min.substring(j,z);
<span style="white-space:pre">				</span>if (max.indexOf(tem)!=-1)   return tem;
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return "";
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 字符数组反转
<span style="white-space:pre">	</span> * @param c  需要反正的字符数组
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>private static void reverse(char[] c,int x,int y){
<span style="white-space:pre">		</span>for (int start = x,end=y-1; start < end; start++,end--) {
<span style="white-space:pre">			</span>swape(c,start,end);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 置换操作---用于置换字符数组
<span style="white-space:pre">	</span> * @param c     字符数组 
<span style="white-space:pre">	</span> * @param start  置换位置start
<span style="white-space:pre">	</span> * @param end    置换位置end
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>private static void swape(char[] c, int start, int end) {
<span style="white-space:pre">		</span>char tem =c[start];
<span style="white-space:pre">		</span>c[start] =c[end];
<span style="white-space:pre">		</span>c[end]   = tem;
<span style="white-space:pre">	</span>}
}




总结:

String类的使用就介绍这么多,其它的方法以及这里到的方法的详细声明可以参看对应的API文档。String方法都比较简单,多练习练习就熟练了。





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值