String类型转换

练习代码

<span style="font-size:14px;">/**
 * String字符串
 * @author admin
 *
 */
public class StringEx {
	public static void main(String[] args) {
		String str = "abc";
		//将数组转换为字符串
		char[] cs = {'a','b','c'};
		String s = new String(cs);
		System.out.println(s);
		//将字符串转换为数组
		char[] charArray = str.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			System.out.print(charArray[i]+",");
		}
		
		char c = str.charAt(1);
		System.out.println(c);
		
		int codePointAt = str.codePointAt(1);
		int codePointBefore = str.codePointBefore(1);
		sop(codePointAt);
		sop(codePointBefore);
		
		String s2 = "happy";
		sop(str.concat(s2));        
		sop(str+s2);                //开发常用拼接
		
		sop(s2.contains("p"));        //判断是否包含
		
		sop(s2.contentEquals("app"));          
		
		/*char[] cha = {'e','a'};
		sop(s2.copyValueOf(cha));*/
		
		String javas = "dadda.java";
		sop(javas.endsWith(".java"));       //判断文件类型
		
		String q = "hua";
		String w = "HUA";
		sop(q.equals(w));
		sop(q.equalsIgnoreCase(w));          //验证码,不区分大小写
		
		byte[] byt = q.getBytes();
		for (int i = 0; i < byt.length; i++) {
			System.out.print(byt[i]+"--");
		}
		
	}
	//打印对象
	public static void sop(Object obj){
		System.out.println(obj);
	}
}</span>
StringBuffer

<span style="font-size:14px;">/**
 * stringbuffer实例
 * @author admin
 *
 */
public class StringExchange {
	public static void main(String[] args) {
		StringBuffer buffer2 = new StringBuffer();
		System.out.println(buffer2.capacity());        //自定义长度
		buffer2.append("你好,新世界");
		
		StringBuffer buffer = new StringBuffer();
		char[] ch = {'h','a','o'};
		buffer.append("nihao").append("shijie");
		buffer.append(ch);
		buffer.append(23);
		buffer.append(buffer2);
		
		buffer.appendCodePoint(56);
		
		System.out.println(buffer.toString());
		System.out.println(buffer.capacity());     //字符长度
		
		System.out.println(buffer.charAt(5));      //脚标点
		System.out.println(buffer.codePointCount(2, 8));   //字符串长度
		System.out.println(buffer.delete(3, 8));
		System.out.println(buffer.deleteCharAt(3));  //指定字符删除
		System.out.println(buffer.insert(1, "中"));    //指定添加
		
		System.out.println(buffer.offsetByCodePoints(4, 8));
		System.out.println(buffer.replace(2, 4, "夏目玲子"));  //替代字符串
		
	}
	

}</span>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用以下两种方法将String类型转换为double类型: 1. 使用Double.parseDouble(String str)方法:这是将String转换为double的常用方法。这个方法接受一个表示数字的字符串作为参数,并返回一个对应的double类型的值。如果输入的字符串无法解析为有效的double值,则会抛出NumberFormatException异常。例如: ```java String str = "3.14"; double num = Double.parseDouble(str); System.out.println(num); // 输出: 3.14 ``` 2. 使用Double.valueOf(String str)方法:这个方法与parseDouble()方法类似,也可以将String转换为double类型。不同之处在于,valueOf()方法返回一个Double对象,而不是原始的double类型。需要注意的是,如果输入的字符串无法解析为有效的double值,也会抛出NumberFormatException异常。例如: ```java String str = "3.14"; Double num = Double.valueOf(str); System.out.println(num); // 输出: 3.14 ``` 需要注意的是,在进行String到double的转换时,要确保输入的字符串表示一个有效的数字。否则,会抛出NumberFormatException异常。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Double和String互转](https://blog.csdn.net/weixin_44057635/article/details/124527703)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值