day15-String类的API和StringBuffer类的API

本文介绍了Java中String和StringBuffer类的各种实用方法,包括字符串的查找、替换、截取等操作,以及如何使用StringBuffer进行高效的数据拼接和修改。

String对象的API

package cn.itcast.demo02;
/*
 *  String类的查找功能
 */
public class StringDemo4 {
	public static void main(String[] args) {
		function_9();
	}
	/*
	 *  boolean equals(Object obj)
	 *  方法传递字符串,判断字符串中的字符是否完全相同,如果完全相同返回true
	 *  
	 *  boolean equalsIgnoreCase(String s)
	 *  传递字符串,判断字符串中的字符是否相同,忽略大小写
	 */
	public static void function_9(){
		String str1 = "Abc";
		String str2 = "abc";
		//分别调用equals和equalsIgnoreCase
		boolean b1 = str1.equals(str2);
		boolean b2 = str1.equalsIgnoreCase(str2);
		System.out.println(b1);
		System.out.println(b2);
	}
	
	/*
	 * char[] toCharArray() 将字符串转成字符数组
	 * 功能和构造方法相反
	 */
	public static void function_8(){
		String str = "itcast";
		//调用String类的方法toCharArray()
		char[] ch = str.toCharArray();
		for(int i = 0 ; i < ch.length ; i++){
			System.out.println(ch[i]);
		}
	}
	
	/*
	 *  byte[] getBytes() 将字符串转成字节数组
	 *  此功能和String构造方法相反
	 *  byte数组相关的功能,查询编码表
	 */
	public static void function_7(){
		String str = "abc";
		//调用String类方法getBytes字符串转成字节数组
		byte[] bytes = str.getBytes();
		for(int i = 0 ; i < bytes.length ; i++){
			System.out.println(bytes[i]);
		}
	}
	
	/*
	 *  int indexOf(char ch)
	 *  查找一个字符,在字符串中第一次出现的索引
	 *  被查找的字符不存在,返回-1
	 */
	public static void function_6(){
		String str = "itcast.cn";
		//调用String类的方法indexOf
		int index = str.indexOf('x');
		System.out.println(index);
	}
	
	/*
	 *  boolean contains (String s)
	 *  判断一个字符串中,是否包含另一个字符串
	 */
	public static void function_5(){
		String str = "itcast.cn";
		//调用String类的方法contains
		boolean b =str.contains("ac");
		System.out.println(b);
	}
	
	/*
	 * boolean endsWith(String prefix)
	 * 判断一个字符串是不是另一个字符串的后缀,结尾
	 * Demo.java
	 *     .java
	 */
	public static void function_4(){
		String str = "Demo.java";
		//调用String类方法endsWith
		boolean b = str.endsWith(".java");
		System.out.println(b);
	}
	
	/*
	 * boolean startsWith(String prefix)  
	 * 判断一个字符串是不是另一个字符串的前缀,开头
	 * howareyou
	 * hOw
	 */
	  public static void function_3(){
		  String str = "howareyou";
		  //调用String类的方法startsWith
		  boolean b = str.startsWith("hOw");
		  System.out.println(b);
	  }
	
	/*
	 *  String substring(int beginIndex,int endIndex) 获取字符串的一部分
	 *  返回新的字符串
	 *  包含头,不包含尾巴
	 *  
	 *  String substring(int beginIndex)获取字符串的一部分
	 *  包含头,后面的字符全要
	 */
	public static void function_2(){
		String str = "howareyou";
		//调用String类方法substring获取字符串一部分
		str= str.substring(1, 5);
		System.out.println(str);
		
		String str2 = "HelloWorld";
		str2 = str2.substring(1);
		System.out.println(str2);
	}
	
	/*
	 *  int length() 返回字符串的长度
	 *  包含多少个字符
	 */
	public static void function(){
		String str = "cfxdf#$REFewfrt54GT";
		//调用String类方法length,获取字符串长度
		int length = str.length();
		System.out.println(length);
	}
}

StringBuffer类的API

package cn.itcast.demo03;

public class StringBufferDemo {
	public static void main(String[] args) {
		function_5();
	}
	/*
	 *  StringBuffer类的方法
	 *   String toString() 继承Object,重写toString()
	 *   将缓冲区中的所有字符,变成字符串
	 */
	public static void function_5(){
		StringBuffer buffer = new StringBuffer();
		buffer.append("abcdef");
		buffer.append(12345);
		
		//将可变的字符串缓冲区对象,变成了不可变String对象
		String s = buffer.toString();
		System.out.println(s);
	}
	
	/*
	 *  StringBuffer类的方法
	 *    reverse() 将缓冲区中的字符反转
	 */
	public static void function_4(){
		StringBuffer buffer = new StringBuffer();
		buffer.append("abcdef");
		
		buffer.reverse();
		
		System.out.println(buffer);
	}
	
	/*
	 *  StringBuffer类方法
	 *    replace(int start,int end, String str)
	 *    将指定的索引范围内的所有字符,替换成新的字符串
	 */
	public static void function_3(){
		StringBuffer buffer = new StringBuffer();
		buffer.append("abcdef");
		
		buffer.replace(1, 4, "Q");
		
		System.out.println(buffer);
	}
	
	/*
	 *  StringBuffer类方法 insert
	 *    insert(int index, 任意类型)
	 *  将任意类型数据,插入到缓冲区的指定索引上
	 */
	 public static void function_2(){
		 StringBuffer buffer = new StringBuffer();
		 buffer.append("abcdef");	 
		 
		 buffer.insert(3, 9.5);
		 System.out.println(buffer);
	 }
	
	/*
	 * StringBuffer类方法
	 *   delete(int start,int end) 删除缓冲区中字符
	 *   开始索引包含,结尾索引不包含
	 */
	public static void function_1(){
		StringBuffer buffer = new StringBuffer();
		buffer.append("abcdef");
		
		buffer.delete(1,5);
		System.out.println(buffer);
	}
	
	/*
	 *  StringBuffer类方法
	 *   StringBuffer append, 将任意类型的数据,添加缓冲区
	 *   append 返回值,写return this
	 *   调用者是谁,返回值就是谁
	 */
	public static void function(){
		StringBuffer buffer = new StringBuffer();
		//调用StringBuffer方法append向缓冲区追加内容
		buffer.append(6).append(false).append('a').append(1.5);
		System.out.println(buffer);
	}
}


【电动汽车充电站有序充电调度的分散式优化】基于蒙特卡诺拉格朗日的电动汽车优化调度(分时电价调度)(Matlab代码实现)内容概要:本文介绍了基于蒙特卡洛拉格朗日方法的电动汽车充电站有序充电调度优化方案,重点在于采用分散式优化策略应对分时电价机制下的充电需求管理。通过构建数学模型,结合不确定性因素如用户充电行为电网负荷波动,利用蒙特卡洛模拟生成大量场景,并运用拉格朗日松弛法对复杂问题进行分解求解,从而实现全局最优或近似最优的充电调度计划。该方法有效降低了电网峰值负荷压力,提升了充电站运营效率与经济效益,同时兼顾用户充电便利性。 适合人群:具备一定电力系统、优化算法Matlab编程基础的高校研究生、科研人员及从事智能电网、电动汽车相关领域的工程技术人员。 使用场景及目标:①应用于电动汽车充电站的日常运营管理,优化充电负荷分布;②服务于城市智能交通系统规划,提升电网与交通系统的协同水平;③作为学术研究案例,用于验证分散式优化算法在复杂能源系统中的有效性。 阅读建议:建议读者结合Matlab代码实现部分,深入理解蒙特卡洛模拟与拉格朗日松弛法的具体实施步骤,重点关注场景生成、约束处理与迭代收敛过程,以便在实际项目中灵活应用与改进。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值