多个字符串进⾏ASCII排序⼀、最近做项⽬需要对多个字符串进⾏ASCII从⼩到⼤排序加密,写个随笔记录下学习,代码如下:/**
* 字符串⼯具类
* @author skyhcwwu
*/
public class StringUtil {
public static void main(String[] args) {
String[] source1 = new String[]{"demo", "about", "count", "resolove02", "resolove01", "aboutting", "basic", "abist"}; println("排序前String[]:\n" + converToString(source1));
// 排序
sortString(source1);
}
/**
* 对传⼊的字符串数组进⾏ASCII排序
* @param source 字符串数组
* @return
*/
public static String[] sortString(String[] source) {
String str1 = "";
String str2 = "";
String temp = "";
int length = 0;
for (int i = 0; i < source.length; i++) {
for (int m = 0; m < source.length - 1; m++) {
str1 = source[m];
str2 = source[m + 1];
length = str1.length() > str2.length() ? str2.length() : str1.length();
for (int j = 0; j < length; j++) {
if (str1.charAt(j) == str2.charAt(j)) {
continue;
}else if (str1.charAt(j) < str2.charAt(j)) {
break;
} else {
temp = str1;
source[m] = str2;
ascii共有多少个字符source[m + 1] = temp;
}
}
}
}
return source;
}
public static String converToString(String[] source) {
StringBuffer sb = new StringBuffer();
String str1 = "";
for (int k = 0; k < source.length; k++) {
str1 = source[k];
if (k != source.length -1 ) {
sb.append(str1).append(", ");
} else {
sb.append(str1);
}
}
String();
}
}
⼆、执⾏main⽅法后得到的结果:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论