day15总结(1. toString()、equals()方法 2. String、StringBuilder常用方法)

本文详细介绍了Java中字符串类String的基本特性和常用方法,包括字符串比较、操作及转换等,并对比了String与StringBuilder/StringBuffer的区别,适用于初学者及需要复习相关知识点的开发者。
Object: 所有类的直接或间接父类
toString: 返回对象的字符串表现形式     Object中以地址返回   对象数据类型  @ 地址    子类可以重写成根据内容返回      alt + shift + s  
equals: 比较调用方法的对象与传入的对象是否相同     Object中比较对象地址值   子类可以重写成比较内容   alt + shift + s 

String: 字符串类,字符串是常量;它们的值在创建之后不能更改

构造 new String(char [] chs)、new String(byte [] bys)、new String(StringBuilder sb);   构造可直接将如上常用类型转换为字符串

方法

  ★ boolean equals(Object obj) 判断两个字符串中的内容是否相同
boolean equalsIgnoreCase(String str)  判断两个字符串中的内容是否相同, 忽略大小写
  ★ boolean contains(String str) 判断该字符串中 是否包含给定的字符串
boolean startsWith(String str) 判断该字符串 是否以给定的字符串开头
boolean endsWith(String str) 判断该字符串 是否以给定的字符串结尾
boolean isEmpty() 判断该字符串的内容是否为空的字符串  ""
   ★ int length() 获取该字符串的长度
 ★ String[] split(String regex) 根据给定的正则表达式来拆分字符串 str.split(regex)
char charAt(int index) 获取该字符串中指定位置上的字符 
           ★ String substring(int start) 从指定位置开始,到末尾结束,截取该字符串,返回新字符串
String substring(int start,int end) 从指定位置开始,到指定位置结束,截取该字符串,返回新字符串 
int indexOf(int ch ) 获取给定的字符,在该字符串中第一次出现的位置
int indexOf(String str) 获取给定的字符串,在该字符串中第一次出现的位置
int indexOf(int ch,int fromIndex) 从指定位置开始,获取给定的字符,在该字符
 ★ byte[] getBytes() 把该字符串 转换成 字节数组
char[] toCharArray() 把该字符串 转换成 字符数组
 ★ String replace(char old,char new) 在该字符串中,将给定的旧字符,用新字符替换
String replace(String old,String new) 在该字符串中, 将给定的旧字符串,用新字符串替换
String trim() 去除字符串两端空格,中间的不会去除,返回一个新字符串
String toLowerCase() 把该字符串转换成 小写字符串 
String toUpperCase() 把该字符串转换成 大写字符串
int indexOf(String str,int fromIndex) 从指定位置开始,获取给定的字符串,在该字符串中第一次出现的位置


StringBuffer/StringBuilder:
方法
public StringBuffer append(String str) 在原有字符串缓冲区内容基础上,在末尾追加新数据
public StringBuffer insert(int offset,String str) 在原有字符串缓冲区内容基础上,在指定位置插入新数据
public StringBuffer deleteCharAt(int index) 在原有字符串缓冲区内容基础上,删除指定位置上的字符
public StringBuffer delete(int start,int end) 在原有字符串缓冲区内容基础上,删除指定范围内的多个字符
public StringBuffer replace(int start,int end,String str)在原有字符串缓冲区内容基础上,将指定范围内的多个字符 用给定的字符串替换
    ★ public StringBuffer reverse() 将字符串缓冲区的内容 反转  "abc"----"cba"
      ★ public String substring(int start) 从指定位置开始,到末尾结束,截取该字符串缓冲区,返回新字符串
    ★ public String substring(int start,int end)  从指定位置开始,到指定位置结束,截取该字符串缓冲区,返回新字符串
log.info("=== Order 大端数据解析 ==="); buffer.order(ByteOrder.BIG_ENDIAN); // 1. 地址读取 mapinfo.put("sendAddress", Byte.toUnsignedInt(buffer.get())); mapinfo.put("targetAddress", Byte.toUnsignedInt(buffer.get())); // 2. 保存当前缓冲区起始位置 final int startPos = buffer.position(); // 3. 查找 "trak" 模式 (0x7472616b) final byte[] trakPattern = {116, 114, 97, 107}; int trakIndex = -1; // 优化:使用滑动窗口直接比较字节数组 for (int i = buffer.position(); i <= buffer.limit() - 4; i++) { boolean match = true; for (int j = 0; j < 4; j++) { if (buffer.get(i + j) != trakPattern[j]) { match = false; break; } } if (match) { trakIndex = i - startPos; // 计算相对偏移量 break; } } if (trakIndex == -1) { mapinfo.put("decryptedData", "未找到 trak 标识"); return; } // 4. 提取加密数据(从当前位置到trak标识) ByteBuffer encryptedBuffer = buffer.slice().order(ByteOrder.BIG_ENDIAN); encryptedBuffer.limit(trakIndex); // 5. 解密处理 byte[] encryptedBytes = new byte[encryptedBuffer.remaining()]; encryptedBuffer.get(encryptedBytes); String content_encrpy = Utility.bytesToHexString(encryptedBytes); String decryptedData = decryptProtobufMy(content_encrpy, client_id, task_id); if (decryptedData == "") { mapinfo.put("functionCode", buffer.get() & 0xFF); mapinfo.put("commandCode", buffer.get() & 0xFF); if(!mapinfo.get("commandCode").toString().equals("6")&&!mapinfo.get("commandCode").toString().equals("7")){ try { throw new Exception("解析异常!"); } catch (Exception e) { e.printStackTrace(); } } mapinfo.put("taskId", buffer.getInt() & 0xFFFFFFFFL); int dataLength = buffer.getShort() & 0xFFFF; mapinfo.put("dataLength", dataLength); if(mapinfo.get("commandCode").toString().equals("6")){ // 1.履历条目 - U16 mapinfo.put("RecordIndex", buffer.getShort() & 0xFFFF); // 2.记录事件类型 - U16 mapinfo.put("EventType", buffer.getShort() & 0xFFFF); // 3.秒 - U8 mapinfo.put("Second", buffer.get() & 0xFF); // 4.分钟 - U8 mapinfo.put("Minute", buffer.get() & 0xFF); // 5.小时 - U8 mapinfo.put("Hour", buffer.get() & 0xFF); // 6.天 - U8 mapinfo.put("Day", buffer.get() & 0xFF); // 7.月 - U8 mapinfo.put("Month", buffer.get() & 0xFF); // 8.年 - U8 mapinfo.put("Year", buffer.get() & 0xFF); // 9.总电压 - U32 mapinfo.put("TotalVoltage", (buffer.getInt() & 0xFFFFFFFFL)); // 10.最大电压 - U16 mapinfo.put("MaxVoltage", buffer.getShort() & 0xFFFF); // 11.最小电压 - U16 mapinfo.put("MinVoltage", buffer.getShort() & 0xFFFF); // 12.电流 - S32 mapinfo.put("Current", buffer.getInt()); // 13.最高温度 - S8 mapinfo.put("MaxTemperature", (byte) buffer.get()); // 14.最低温度 - S8 mapinfo.put("MinTemperature", (byte) buffer.get()); // 15.循环次数 - U16 mapinfo.put("CycleCount", buffer.getShort() & 0xFFFF); // 16.剩余容量 - U32 mapinfo.put("RemainingCapacity", (buffer.getInt() & 0xFFFFFFFFL)); // 17.满充容量 - U32 mapinfo.put("FullChargeCapacity", (buffer.getInt() & 0xFFFFFFFFL)); // 18.放电SOP - U16 (0.1A) mapinfo.put("DischargeSOP", (buffer.getShort() & 0xFFFF) * 0.1); // 19.充电SOP - U16 (0.1A) mapinfo.put("ChargeSOP", (buffer.getShort() & 0xFFFF) * 0.1); // 20.电池故障码1 - U32 long unsignedValue = buffer.getInt() & 0xFFFFFFFFL; String batteryFaultCode1 = String.format("%08X", unsignedValue); mapinfo.put("BatteryFaultCode1", batteryFaultCode1); // 21.电池故障码2 - U32 long unsignedValue2 = buffer.getInt() & 0xFFFFFFFFL; String batteryFaultCode2 = String.format("%08X", unsignedValue2); mapinfo.put("BatteryFaultCode2", batteryFaultCode2); // 22.电池故障码3 - U32 long unsignedValue3 = buffer.getInt() & 0xFFFFFFFFL; String batteryFaultCode3 = String.format("%08X", unsignedValue3); mapinfo.put("BatteryFaultCode3", batteryFaultCode3); // 23.最大电压通道 - U8 mapinfo.put("MaxVoltageChannel", buffer.get() & 0xFF); // 24.最小电压通道 - U8 mapinfo.put("MinVoltageChannel", buffer.get() & 0xFF); // 25.最大温度通道 - U8 mapinfo.put("MaxTempChannel", buffer.get() & 0xFF); // 26.最小温度通道 - U8 mapinfo.put("MinTempChannel", buffer.get() & 0xFF); // 27.软件版本号第一位 - U8 mapinfo.put("SoftwareVersion1", buffer.get() & 0xFF); // 28.软件版本号第二位 - U8 mapinfo.put("SoftwareVersion2", buffer.get() & 0xFF); // 29.测试版本号第一位 - U8 mapinfo.put("TestVersion1", buffer.get() & 0xFF); // 30.测试版本号第二位 - U8 mapinfo.put("TestVersion2", buffer.get() & 0xFF); // 31.预留 - U32 mapinfo.put("Reserved", (buffer.getInt() & 0xFFFFFFFFL)); // 32.BMS工作状态 - U8 mapinfo.put("BMSStatus", buffer.get() & 0xFF); } if(mapinfo.get("commandCode").toString().equals("7")){ // 读取 dataLength 个字节 byte[] dataBytes = new byte[dataLength]; buffer.get(dataBytes); // 转为十六进制字符串(用于显示) StringBuilder hexBuilder = new StringBuilder(); for (byte b : dataBytes) { hexBuilder.append(String.format("%02X", b & 0xFF)); } String hexString = hexBuilder.toString(); int decimalValue = Integer.parseInt(hexString, 16); mapinfo.put("data", String.valueOf(decimalValue)); } mapinfo.put("crcCode", buffer.getShort() & 0xFFFF); // 2字节 CRC 校验码 return; } // 6. 解析解密数据 byte[] decBytes = Utility.hexStringToBytes(decryptedData); ByteBuffer buf = ByteBuffer.wrap(decBytes).order(ByteOrder.BIG_ENDIAN); mapinfo.put("functionCode", buf.get() & 0xFF); mapinfo.put("commandCode", buf.get() & 0xFF); if(!mapinfo.get("commandCode").toString().equals("3")&&!mapinfo.get("commandCode").toString().equals("4")){ throw new Exception("解析异常!"); } mapinfo.put("taskId", buf.getInt() & 0xFFFFFFFFL); int dataLength = buf.getShort() & 0xFFFF; mapinfo.put("dataLength", dataLength); // 读取 dataLength 个字节 byte[] dataBytes = new byte[dataLength]; buf.get(dataBytes); // 转为十六进制字符串(用于显示) StringBuilder hexBuilder = new StringBuilder(); for (byte b : dataBytes) { hexBuilder.append(String.format("%02X", b & 0xFF)); } String hexString = hexBuilder.toString(); int decimalValue = Integer.parseInt(hexString, 16); mapinfo.put("data", String.valueOf(decimalValue)); mapinfo.put("crcCode", buf.getShort() & 0xFFFF); // 2字节 CRC 校验码 这commandCode=6、7的时候不需要解密,但是commandCode=7的时候又揭秘成功,导致commandCode=7处理错误,如何解决
最新发布
10-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值