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

JAVA基础之String字符串笔记总结

2013-04-06 23:38 423 查看
-------android培训java培训、期待与您交流! ----------

 

 

  1. 判断2个字符串是否相同
  public boolean equals(String)覆盖了Object中的equals的方法,比较的是内容
 
   2. 判断2个字符串是否相同,忽略大小写
       public boolean equalsIgnoreCast(String anotherString)
 
   3. 判断一个字符串中是否包含另一个字符串   jdk1.5开始有的
      public boolean contains(String) 
 
  4. 测试此字符串是否以指定的后缀结束。 
     public boolean endsWith(String suffix)
  
  5. 判断一个字符串,是否以另一个字符串开始
     public boolean startsWith(String suffix)
  
  6. 判断一个字符串是否为空
      public boolean isEmpty(String)
 
//代码示例

class StringDemo

{
public static void main(String[] args)
{
//判断2个字符串是否相同
String s1 = "abcdefg";
String s2 = "abcdefg";
System.out.println(s1.equals(s2));

//判断2个字符串是否相同,忽略大小写
String s3 = "abcdefg";
String s4 = "abcDefG";
System.out.println(s3.equalsIgnoreCase(s4));

//判断一个字符串是否包含另一个字符串
String s5 = "hshdsdf";
String s6 = "sdf";
System.out.println(s5.contains(s6));

//判断一个字符串是否以另一个字符串开始
String s7 = "abcdefg";
String s8 = "abcd";
System.out.println(s7.startsWith(s8));

//判断一个字符串是否以另一个字符串结尾
String s9 = "abcdefg";
String s10 = "ef";
System.out.println(s9.endsWith(s10));

//判断一个字符串是否为空
String s11 = "abcdef";
String s12 = "";
System.out.println(s11.isEmpty());
System.out.println(s12.isEmpty());

}

}

 * 获取:

 * 1. 指定索引位置,查找相对应的字符

 *    public char charAt(int index)

 *  

 * 2. 指定字符,查找字符在字符串中的下标,找不到就是-1 

 *  public int indexOf(int i)

 *  public int indexOf(int i,int indexfrom)  从指定位置搜索,但是字符串的下标示不会被改变

 *  public int indexOf(String str)  搜索,指定的字符串是否在原来的字符串中出现过

 *  public int indexOf(String str,int indexfrom)  从指定位置搜索,指定的字符串是否在原来的字符串中出现过

 *  

 * 3. lastIndexOf() 和 indexOf功能一致,用法一致,意义在于这个功能,是反向索引

 *  public int lastIndexOf(int i)

 *  

 * 4. 获取字符串的长度

 *  public int length()

 *  

 * 5. 获取字符串中的一部分,返回新串 *** 

 *  public String substring(int start,int end)包含头,不包含尾,原始字符串不变

 * 

 * 

 * 

 * 转换

 * 1. 将字符串转成字节数组

 * public byte[] getBytes()

 * 此功能目前用不到,在IO流中会用

 * 

 * 2. 将字符串转成字符数组

 * public char[] toCharArray()

 *    将字符数组转成字符串怎么转  构造方法  new String(char[] ch)

 *    

 * 3. 将字符串进行大小写转换  原始字符串不变

 *  public String toUpperCase()转成大写

 *  public String toLowerCase()转成小写

 *  

 * 4. 将字符数组转成字符串

 *  public static String copyValueOf(char[] ch)

 * 

 * 5. 将基本数据类型转成字符串 

 *  public static String valueOf(基本类型)

 *  public static String valueOf(5)

 *  public static String valueOf(char[] ch)

 * 


1,存储。

StringBuffer append():将指定数据作为参数添加到已有数据结尾处。

StringBuffer insert(index,数据):可以将数据插入到指定index位置。

2,删除。

StringBuffer delete(start,end):删除缓冲区中的数据,包含start,不包含end。

StringBuffer deleteCharAt(index):删除指定位置的字符。

3,获取。

//指定索引位置,查找对应的字符

char charAt(int index) 

//指定字符,查找字符在字符串的下标

int indexOf(String str) 

int lastIndexOf(String str) 

int length() 

String substring(int start, int end) 

4,修改。

StringBuffer replace(start,end,string);

void setCharAt(int index, char ch) ;

5,反转。

StringBuffer reverse();

6,将缓冲区中指定数据存储到指定字符数组中。

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 

//代码示例

 class StringDemo02

{
public static void main(String[] args)
{
//指定索引位置,查找对应的字符
String s1 = "hwlndty";
System.out.println(s1.charAt(4));

//指定字符,查找字符在字符串中出现的下标,找不到就是-1
String s2 = "hwlndty";
System.out.println(s2.indexOf("d"));

//从指定位置搜索,查找字符在字符串中出现的下标
System.out.println(s2.indexOf("t",3));

// 从指定位置搜索,指定的字符串是否在原来的字符串中出现过
System.out.println("s2......"+s2.indexOf("nd"));

//从指定位置搜索,指定的字符串是否在原来的字符串中出现过
System.out.println("s2......"+s2.indexOf("ty",2));

//获取字符串的长度
       System.out.println("s2.length="+s2.length());

//获取字符串中的一部分,返回新串 ,包含头,不包含尾,原始字符串不变
System.out.println("s2.substring="+s2.substring(2,6));

// 将字符串转换成字符数组
//public char[] toCharArray()

String s3 = "welcome to China!";
char[] ch = s3.toCharArray();
for (int i = 0; i < ch.length; i++)
{
System.out.print(ch[i]+",");
}
System.out.println();

//将字符串进行大小写转换,原始字符不变
//public String toUpperCase()
//public String toLowerCase()
String s4 = "hello JAVA!";

System.out.println("toUpperCase..."+s4.toUpperCase());
System.out.println("toLowerCase..."+s4.toLowerCase());

//将字符数组转换成字符串
//public static String copyValueOf(char[] ch)
char[] ch2 = {'d','f','g','t','k','u','o','e','p','m'};
String ss1 =String.copyValueOf(ch2);
System.out.println("ss1..."+ss1);

//char[] ch3 = {'d','f','g','t','k'};
boolean b = true;
String ss2 = String.valueOf(b);
System.out.println("ss2..."+ss2);

}

}

//代码示例

class StringTest

{
public static void main(String[] args)
{
//字符串的替换
String s1 = "hlkjhkgf";
String ss1 = s1.replace("k","*");
System.out.println("ss1..."+ss1);

//字符串的切割
String s2 = "how,do,you,do";
String[] ss2 = s2.split(",");
System.out.println("ss2..."+ss2.length);
for (int i = 0; i < ss2.length; i++) 
{
System.out.println(ss2[i]);
}

//去掉字符串2端的空格
//public String trim()
String s3= "  where are you now ";
System.out.println("s3..trim..."+s3.trim()+"!");

//按照字典顺序比较字符串
String s4 = "cdef";
String s5 = "cded";
System.out.println(s4.compareTo(s5));

  //查找字符在字符串中出现了几次
   String s6 = "sdghfqwghuytghjk";
   int count = getCount("sdghfqwghuytghjk","gh");
  System.out.print(count);
  System.out.println();

 //StringBuffer添加数据
  StringBuffer sb = new StringBuffer("howdoyoudo");
 
  sb.append("abc");
  System.out.println("sb="+sb);
  
  sb.insert(sb.length(),"AA");
  System.out.println("sb="+sb);
  
  //StringBuffer在指定位置替换字符
  sb.setCharAt(3,'A');
  System.out.println(sb);

  //StringBuffer删除字符串指定位置元素
  sb.deleteCharAt(4);
  System.out.println(sb);
  
  //反转字符串
  sb.reverse();
  System.out.println(sb);
  
  //清空字符串
  sb.setLength(0);
  System.out.println("sb="+sb);

}

private static int getCount(String str, String sub)
{
int count=0;
int index=0;
while((index = str.indexOf(sub))!=-1)
{
str = str.substring(index+sub.length(), str.length());
count++;
}
return count;
}

}

 

基本数据类型包装类

int--Integer:

short--Short

byte--Byte

long--Long

double--Double

char--Charator

float--Float

boolean--Boolean

MAX_VALUE 获取整型最大值

MIN_VALUE 获取整型最小值

toBinaryString 转换为二进制

toHexString 转换为十六进制

toOctalString 转换为八进制

Integer.ParseInt(String)字符串转成整数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JAVA 笔记 总结 string