黑马程序员----【javaSE基础】入门基础--字符串

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



学过编程的都会知道,字符串是常量中的一种,然而java中的String不仅仅是常量,又是类,是一个特殊的类,是不可变和最终类;将字符串作为内置的对象处理允许Java提供十分丰富的功能特性以方便处理字符串。下面是我自己练习的一些使用频率比较高的函数及其相关说明。

public class Stringex{
	public static void main(String[] args)
	{
		//----------声明创建字符串变量 ---------------
		//(1).可以为字符串变量赋值简单的字符串常量来完成初始化 
		String str1="My number";
		System.out.println(str1);
		
		//(2). String()构造方法 
		String str2;
		str2=new String("  is ");
		System.out.println(str2);
		
		//(3).  String(byte[ ] bytes)构造方法
		byte[] byteArray=new byte[]{52,49,51}; // 创建字节数组
		String str3=new String(byteArray);
		System.out.println(str3);
		
		//(4). String(String  charsetName)构造方法 
		String str4=new String(". ");
		System.out.println(str4);
		
		//-----------字符串操作---------------------------
		//(1). 获取字符串长度 
		System.out.println("(1).The str1's length:"+str1.length());
		
		//(2). 字符串查找 
		//indexOf(String s) 返回指定子字符串在此字符串中第一次出现处的索引
		System.out.println("(2).The letter 'e' 's index:  "+str1.indexOf("n"));

		//(3). lastIndexOf(String str) 返回指定子字符串在此字符串中最右边出现处的索引。
		System.out.println("(3).The letter 'r' 's the right index:  "+str1.indexOf("r"));
		
		//(4). 获取指定索引位置的字符 
		//str.charAt(int index) 
		System.out.println("(4). The index of 3's letter is:  "+str1.charAt(3));
		
		//(5). 获取子字符串
		//substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。
		System.out.println("(5).Behind the index of 3's substring is:  "+str1.substring(3));
		//substring(int beginIndex , int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。
		System.out.println("(5).Between the index  5 and 8's substring is:  "+str1.substring(5,8));
		
		//(6). 去除空格 
		//trim()方法返回字符串的副本,忽略前导空白和尾部空格
		System.out.println("(6).After delete space,the new string is:"+str2.trim()+str3);
		
		//(7).字符串替换
		//str.replace(char oldChar,char newChar); replace()方法可实现将指定的字符或字符串替换成新的字符或字符串 
		System.out.println("(7).The new String is:  "+str1.replace("My","my"));
		
		//(8). 判断字符串是否相等str.equals(String otherstr)  
		//如果两个字符串具有相同的字符和长度,则使用equals()方法进行比较时,则返回true。
		System.out.println("(8)."+str3.equals("413"));
		
		//(9).字母大小写转换
		System.out.println("(9)."+str1.toUpperCase());
		System.out.println("MY name IS".toLowerCase());
		
		//(10).字符串分割str.split(String sign)
		String str5="Today is Tuesday.";
		String[] newstr=str5.split(" ");
		System.out.println("(10).The new string is:");	
		for(int i=0;i<newstr.length;i++)
		{
			System.out.println(newstr[i]);
		}			
	}
}

下面还收集整理了一些很有用的知识

String相关函数

1)substring()
它有两种形式,第一种是:String substring(int startIndex)
第二种是:String substring(int startIndex,int endIndex)

2)concat() 连接两个字符串
例:String s="Welcome to ";
    String t=s.concat("AnHui");
3)replace()替换
它有两种形式,第一种形式用一个字符在调用字符串中所有出现某个字符的地方进行替换,形式如下:
String replace(char original,char replacement)
例如:String s=”Hello”.replace(’l',’w');
第二种形式是用一个字符序列替换另一个字符序列,形式如下:
String replace(CharSequence original,CharSequence replacement)

4)trim() 去掉起始和结尾的空格
5)valueOf() 转换为字符串
6)toLowerCase() 转换为小写
7)toUpperCase() 转换为大写
8)length()取得字符串的长度
例:char chars[]={’a',’b’.’c'};
String s=new String(chars);
int len=s.length();

9)charAt()截取一个字符
例:char ch;
      ch=”abc”.charAt(1); 
       返回值为’b’

10)getChars() 截取多个字符
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart 指定了子串开始字符的下标
sourceEnd 指定了子串结束后的下一个字符的下标。因此,子串包含从sourceStart到sourceEnd-1的字符。
target 指定接收字符的数组
targetStart target中开始复制子串的下标值
例:String s=”this is a demo of the getChars method.”;
char buf[]=new char[20];
s.getChars(10,14,buf,0);

11)getBytes()
替代getChars()的一种方法是将字符存储在字节数组中,该方法即getBytes()
例:
String s = “Hello!你好!”; 
byte[] bytes = s.getBytes();

12)toCharArray()
例:
String s = “Hello!你好!”; 
char[] ss = s.toCharArray();

13)equals()和equalsIgnoreCase() 比较两个字符串
14)regionMatches()用于比较一个字符串中特定区域与另一特定区域,它有一个重载的形式允许在比较中忽略大小写。
boolean regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)
boolean regionMatches(boolean ignoreCase,int startIndex,String
str2,int str2StartIndex,int numChars)

15)startsWith()和endsWith()
startsWith()方法决定是否以特定字符串开始
endWith()方法决定是否以特定字符串结束
16)equals()和==
equals()方法比较字符串对象中的字符
==运算符比较两个对象是否引用同一实例。
例:String s1=”Hello”;
String s2=new String(s1);
s1.eauals(s2); //true
s1==s2;//false

17)compareTo()和compareToIgnoreCase()比较字符串
18)indexOf()和lastIndexOf()
indexOf() 查找字符或者子串第一次出现的地方。
lastIndexOf() 查找字符或者子串是后一次出现的地方。
  19)trim去空格函数
例: String t1 = "     abc    de     ";
  System.out.println(t1.trim());// 去掉开头和结尾的空格“abc de”
20)split 字符串分割
 String y = "abc,de,fg,hi,jk";
  String[] y1 = y.split(",");// 截取字符串所有","字符
  for (int i = 0; i < y1.length; i++) {
   System.out.print(y1[i]);// 输出结果abcdefghijk
  }
21)append 添加或插入函数
 StringBuffer zz1 = new StringBuffer(z1);// append 插入字符
  zz1.append('|').append("hijk").append('/').append("lmn").append("opq");
  System.out.println();
  System.out.print(zz1);// 输出:abcdefg|hijk/lmnopq
StringBuffer构造函数
StringBuffer定义了三个构造函数:
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)

下面是StringBuffer相关的函数:
1)length()和capacity()
一个StringBuffer当前长度可通过length()方法得到,而整个可分配空间通过capacity()方法得到。

2)ensureCapacity() 设置缓冲区的大小
void ensureCapacity(int capacity)

3)setLength() 设置缓冲区的长度
void setLength(int len)

4)charAt()和setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)

5)getChars()
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)

6)append() 可把任何类型数据的字符串表示连接到调用的StringBuffer对象的末尾。
例:int a=42;
StringBuffer sb=new StringBuffer(40);
String s=sb.append(”a=”).append(a).append(”!”).toString();

6)insert() 插入字符串
StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,Object obj)
7)index指定将字符串插入到StringBuffer对象中的位置的下标。

8)reverse() 颠倒StringBuffer对象中的字符
StringBuffer reverse()

9)delete()和deleteCharAt() 删除字符
StringBuffer delete(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)

10)replace() 替换
StringBuffer replace(int startIndex,int endIndex,String str)

11)substring() 截取子串
String substring(int startIndex)
String substring(int startIndex,int endIndex)

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值