Java字符串篇-7-String类方法之转换功能

       这篇继续学习String类的方法,没办法,String类的方法比较多,用得频率也高。这边主要学习几个转换功能的方法。

 

1.Byte[] getBytes() 把字符串转换为字节数组

 

       前面我们学习了一个把字节转换为字符串的方法,就是把一个字节数组,作为参数,使用String类的构造方法,就可以转换为一个字符串对象。现在是要反过来,下面来看一个代码举例。

package string;

public class Demo4_String {

   public static void main(String[] args) {

     String st = "Anthony 你好你好";
     byte[] b = st.getBytes();
     // 遍历这个字节数组
     for (inti = 0; i < b.length; i++) {
        System.out.print(b[i]+ " ");
     }
   }
}

输出结果:65 110 116 104 111 110 121 32 -28 -67 -96 -27 -91 -67 -28 -67 -96 -27 -91-67

       不用怀疑这个结果输出的正确性,Java中byte的范围是-128到127. 后面这些负数的编码其实就是gbk的编码结果,可以发现gbk中一个汉字采用2个字节进行编码。

 

2.Char[] toCharArray() 把字符串转换为字符数组

      这个方法,我们可能使用更多,字符串转换成字符之后就可以进行一些字符的出现次数统计工作。

 

package string;

public class Demo4_String {

   public static void main(String[] args) {
     String st = "Anthony";
     char[] c = st.toCharArray();   // 将字符串转换为字符数组
     for (inti = 0; i < c.length; i++) {
        System.out.print(c[i] + " ");
     }
   }
}

      上面这个方法,给我们提供另外一种字符串遍历的方法。之前,我们是根据索引,调用charAt()的方法去遍历字符串里面字符。现在这是第二个方法,先转换成字符数组,然后遍历这个字符数组。

 

3.String valueOf(char[] ch) 把字符数组装换成字符串

 

      在API 里面找到valueOf方法,发现有好多个这个方法的重载,总结就是,valueOf方法可以把任意的类型数据装换成字符串。

package string;

public class Demo4_String {
   public static void main(String[] args) {
     char[] ch = {'A','n','t','h','o','n','y'};
     String s = String.valueOf(ch);
     System.out.println(s);
   }
}

输出:Anthony

 

注意上面valueOf()是静态方法,所以我们需要使用类名加上点符号去调用。

 

4.String valueOf(int i) 把int类型的数据装换成字符串

 

这个方法是前面一个方法的重载,只是传入参数类型不同。我们来试试一个整数转换成字符串。

package string;

public class Demo4_String {
   public static void main(String[] args) {
     inta = 100;
     intb = 300;
     String s1 = String.valueOf(a);
     String s2 = String.valueOf(b);
     System.out.println(s1 + s2);
   }
}

输出结果是:100300

看输出结果,说明确实把int类型装换成了字符串类型。

 

5.toLowerCase()和toUpperCase()

 

上面两个方法分别是把字符串里面的大写字母转换成小写,把小写字母转换成大写。

package string;

public class Demo4_String {

   public static void main(String[] args) {
     String st = "ADEcedDs";
     String s1 = st.toLowerCase();
     String s2 = st.toUpperCase();
     System.out.println(s1);
     System.out.println(s2);
   }
}

       这种字母大小写转换的场景用得不多,一般是在数据清洗过程中使用,在业务指定业务数据需要按照大写或者小写字母显示,这个时候就有这种方法。

 

6.String concat(String str)

作用是将指定字符串连接到此字符串的结尾。

package string;

public class Demo4_String {
   public static void main(String[] args) {
     String st = "Anthony";
     String s1 = "can writeSelenium script ";
     String s2 = "with Javaand Python.";
     System.out.println(st.concat(s1).concat(s2));
   }
}

输出结果:Anthony can write Selenium script with Java and Python

 

      前面我们知道字符串拼接可以通过加号(+)来实现,那么这两种方法有什么区别吗?应该来说使用+更强大,因为可以和任意其他对象进行拼接转成新字符串,但是使用concat()方法只能是字符串对象调用,而且传入参数必须是字符串对象。


7.来做一个小练习

给定任意一个字符串,需要把该字符串首字母改成大写,后面其余字母都小写。

分析:需要使用上面介绍的大小写转换方法和字符串拼接方法。

package string;

public class Demo4_String {

	public static void main(String[] args) {
		
		transform("anTENIfeeEGEddf");
	}

	public static void transform(String st) {
		String s = st.substring(0,1).toUpperCase().concat(st.substring(1).toLowerCase());
		System.out.println(s);
	}
}

输出结果:Antenifeeegeddf


8.把数组{1,2,3}转成成[1,2,3]格式

package string;

public class Demo4_String {

	public static void main(String[] args) {
		int[] arr = {1,2,3};
		transform(arr);
	}
	public static void transform(int[] arr) {
	
		String s = "[";
		// 把数组遍历拿到进行去拼接字符串
		for (int i = 0; i < arr.length; i++) {
			if(i == arr.length-1) {
				s = s + arr[i] + "]";
			}else {
				s = s+ arr[i] + ",";
			}
		}
		System.out.println(s);
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值