java-----String类型的几种功能

一:String中的判断功能

1: boolean equals(Object anObject);

2:public boolean contains(String str);

3:public boolean startsWith(String prefix);

4:public boolean isEmpty();

二:String类的获取

1:public int length ();

2:public String concat (String str);

3:public char charAt (int index);

4:public int indexOf (String str);

5:public String substring (int beginIndex);

三:String类的转换功能

1:public char[] toCharArray();

2:public byte[] getBytes();

3:public String toUpperCase();

4:public static String valueOf(任意类型);

四:String切割功能

1:public String [ ]spilt(String regex);

五:String的替换功能

1:public String replace(char oldChar,char newChar );

六:String去除两段空格

1:public String trim( );

一:String的判断功能

1:public boolean equals(Object anObject);

比较两个字符串的内容是否相同

package StringTest;

public class StringTest {
public static void main(String[] args) {
	String s1 ="HelloWorld";                //定义字符串
	String s2 = "helloworld";
	String s3 = "helloworld";
	System.out.println(s1.equals(s2));       //判断s1与s2是否相等相等返回true不相等返回false
	System.out.println(s1.equals(s3));
	System.out.println(s2.equals(s3));
    }
}

2:public boolean contains(Char s);

判断指定字符串是否包含指定的字符

package StringTest;

public class StringTest {
public static void main(String[] args) {
	String s1 ="HelloWorld";
	String s2 = "helloworld";
	String s3 = "helloworld";
	System.out.println(s1.contains("lo"));    
    }
}

运行结果:

3:public boolean startsWith(String prefix);

判断字符串是否以指定的前缀开头

package StringTest;

public class StringTest {
public static void main(String[] args) {
	String s1 ="HelloWorld";
	String s2 = "helloworld";
	String s3 = "helloworld";
	System.out.println(s1.startsWith("he"));        //判断字符串是否以指定的前缀开头
	}
}

运行结果:

4:public boolean isEmpty() ;

如果为空就是true (值是空的)

package StringTest;

public class StringTest {
public static void main(String[] args) {
	String s1 ="HelloWorld";
	String s2 = "helloworld";
	String s3 = "helloworld";
	System.out.println(s1.isEmpty());   //判断s1值是否为空;为空就是true
	}
}

运行结果:

二:String类的获取

1:public int length () ;

返回此字符串的长度。

package StringTest;

public class StringTest {
public static void main(String[] args) {
	String str ="HelloWorld";
	System.out.println("长度为:"+str.length());       //获取str字符串的长度。
	}
}

运行结果为:

2:拼接功能: public String concat (String str) ;

     将指定的字符串连接到该字符串的末尾。

package StringTest;

public class StringTest {
public static void main(String[] args) {
	String str ="HelloWorld";
	System.out.println("拼接后结果"+str.concat("java"));
    }
}

运行结果:

3:public char charAt (int index) ;

     返回指定索引处的 char值。

package StringTest;

public class StringTest {
public static void main(String[] args) {
        String str ="HelloWorld";
	System.out.println("返回索引出元素为:"+str.charAt(4));
    }
}

运行结果为:

4:public int indexOf (String str) ;

返回指定子字符串第一次出现在该字符串内的索引。

package StringTest;

public class TestDemo2 {
public static void main(String[] args) {
	String str = "Hello";
	System.out.println("返回指定元素所在的位置为:"+str.indexOf("o"));
    }
}

运行结果为:

5:public String substring (int beginIndex) ;

从开始位置默认截取到结尾,返回截取后的字符串

package StringTest;

public class StringTest {
public static void main(String[] args) {
		String str ="HelloWorld";
	System.out.println("返回截取的字符串为:"+str.substring(3));
	
	}
}

运行结果为:

三:String的转换功能

1:public char[] toCharArray();

将此字符串转换为一个新的字符数组。

package StringTest;

public class TestDemo4 {
public static void main(String[] args) {
	String str ="helloWorld";
	char[]chr = str.toCharArray();
	for(int x=0;x<chr.length;x++) {
		System.out.print(chr[x]);      //输出字符串形式的helloworld
	}

运行结果:

2:public byte[] getBytes();

将字符串转换成字节数

package StringTest;

public class TestDemo4 {
public static void main(String[] args) {
	String str ="helloWorld";
	byte[]by = str.getBytes();            //将字符串转换成字节数
	for(int x=0;x<str.length();x++) {     //重新遍历
		System.out.print(by[x]+" ");      // 输出结果
	}

运行结果:

 

3:public String toUpperCase();

将字符串本身转换大写字符串

package StringTest;

public class TestDemo4 {
public static void main(String[] args) {
	String str ="helloWorld";
System.out.println("转换为大写:"+str.toUpperCase());

运行结果:

4:

public static String valueOf(任意类型数);

可以将任意类型的数

 

package StringTest;

public class TestDemo4 {
public static void main(String[] args) {
	String str ="helloWorld";
	double a= 10.2;
	String str2 = String.valueOf(a);    //将字符串转换为double类型的数字
	System.out.println(str2);
    }
}

运行结果:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值