首页 > 编程知识 正文

java中一个字符串占几个字节

时间:2023-05-03 06:23:09 阅读:241063 作者:271

java判断一个字节数组在另一个字节数组的位置

有时候需要判断,一个字节数组在另一个字节数组的位置,其实只要判断第一位相等,然后后面多位持续比较,就可以判断是否有完全相同的片段。

由于项目需要,自己写了几个byte数组和String类的工具方法,给大家参考使用。

//判断一个字符串source中,有几个字符串srcgetStringCount(String source, String src)//判断一个字符串source中,从指定的位置开始开始计算,字符串src的游标值getStringIndex(String source, String src, int beginIndex)//判断一个byte数组src在另外一个byte数组sources中对应的游标值getByteIndexOf(byte[] sources, byte[] src, int startIndex)//判断一个byte数值src在另外一个byte数组sources中对应的游标值,指定开始的游标和结束的游标位置getByteIndexOf(byte[] sources, byte[] src, int startIndex, int endIndex)//判断一个byte数组src,在另一个byte数组source中存在的个数getByteCountOf(byte[] sources, byte[] src)

简单的示例调用代码:

package com.example.lib2;public class String20180903 { public static void main(String[] args) { //判断一个字符串中是否包含另个字符串 String msg = "-aa-ab-aaa"; String src = "aa"; int count = getStringCount(msg, src); //返回的数量为:3 System.out.println("count:" + count); int index = getStringIndex(msg, src, 2); System.out.println("index:" + index); //返回的游标值为:7 byte[] bytes = {73, 32, 87, 97, 110, 32, 89, 111, 117, 32, 87, 97, 110, 32, 77, 101};//"I Wan You Wan Me" byte[] bytes1={ 87, 97, 110}; //Wan int count2=getByteCountOf(bytes,bytes1); System.out.println("count2:" + count2); //返回的数量为:2 int index2=getByteIndexOf(bytes,bytes1,0); System.out.println("index2:" + index2); //返回的游标值为:2 } //判断一个字符串source中,有几个字符串src private static int getStringCount(String source, String src) { int index = 0; int count = 0; int start = 0; while ((index = source.indexOf(src, start)) != -1) { count++; start = index + 1; } return count; } //判断一个字符串source中,从指定的位置开始开始计算,字符串src的游标值 private static int getStringIndex(String source, String src, int beginIndex) { int index = 0; int start = 0; while ((index = source.indexOf(src, start)) != -1 && index < beginIndex) { start = index + 1; } return index; } //判断一个byte数值在另外一个byte数组中对应的游标值 public static int getByteIndexOf(byte[] sources, byte[] src, int startIndex) { return getByteIndexOf(sources, src, startIndex, sources.length); } //判断一个byte数值在另外一个byte数组中对应的游标值,指定开始的游标和结束的游标位置 public static int getByteIndexOf(byte[] sources, byte[] src, int startIndex, int endIndex) { if (sources == null || src == null || sources.length == 0 || src.length == 0) { return -1; } if (endIndex > sources.length) { endIndex = sources.length; } int i, j; for (i = startIndex; i < endIndex; i++) { if (sources[i] == src[0] && i + src.length < endIndex) { for (j = 1; j < src.length; j++) { if (sources[i + j] != src[j]) { break; } } if (j == src.length) { return i; } } } return -1; } //判断一个byte数组src,在另一个byte数组source中存在的个数 public static int getByteCountOf(byte[] sources, byte[] src) { if (sources == null || src == null || sources.length == 0 || src.length == 0) { return 0; } int count = 0; int start = 0; int index = 0; while ((index = getByteIndexOf(sources, src, start)) != -1) { start = index + 1; count++; } return count; }}

上面就是一些Byte数组和String的功能简单使用,当然如果有相应的功能需求,可以做相应的改变。

共勉:很多人输就输在,对于新兴事物第一看不见,第二看不起,第三看不懂,第四来不及。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。