字符串排序,特殊字符位置不变
来源:互联网 发布:四虎影库软件下载安装 编辑:程序博客网 时间:2024/06/05 16:06
import java.util.Arrays;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.LinkedList;import java.util.List;import java.util.Map.Entry;import java.util.Set;public class SortString {/** * @param args * @author Lzc */public static void main(String[] args) {// TODO Auto-generated method stub//here,must use LinkedList,rather than ArrayListList<Character> list = new LinkedList<Character>(); //save the special character's index and itself//here,must use LinkedHashMap,rather than HashMapHashMap<Integer,Character> hm = new LinkedHashMap<Integer,Character>();StringBuilder sb = new StringBuilder();String str = "sdf*adfasd#ADADFsdfa#fad$ajkldf@sasd%/adf]";String[] strs = str.split("[^a-zA-Z]");int l = 0,i=0;for(String s: strs){System.out.println(s);sb.append(s);l += s.length(); //if((l + i)>=(str.length()-s.length())) break;if(i>strs.length-1) break;//System.out.println(str.charAt(l + i)); //hm.put(str.indexOf(str.charAt(l + i),l+i),str.charAt(l + i)); //hm.put(l+i,str.charAt(l + i)); System.out.println("the key1: " + (l + i) + "--the value1: " + str.charAt(l + i));i++;}char[] chars = sb.toString().toCharArray();Arrays.sort(chars);// sort the char array//list = Arrays.asList(chars);//System.out.println(list.size());for(char c:chars){list.add(c); //add the char to list}//traverse the hashmap,and add all of the special char to its original positionSet<Entry<Integer,Character>> entrySet = hm.entrySet();for(Entry<Integer,Character> e:entrySet){System.out.println("the key2: " + e.getKey() + "--the value2: " + e.getValue());list.add(e.getKey(),e.getValue());}//list to Stringsb = new StringBuilder(); //reset the StringBuilder//traverse the list,append all of the elements in the list to StringBuilderfor(char c:list){sb.append(c);}System.out.println(sb.toString()); }}
test result:
sdfthe key1: 3--the value1: *adfasdthe key1: 10--the value1: #ADADFsdfathe key1: 20--the value1: #fadthe key1: 24--the value1: $ajkldfthe key1: 31--the value1: @sasdthe key1: 36--the value1: %the key1: 37--the value1: /adfthe key1: 41--the value1: ]the key2: 3--the value2: *the key2: 10--the value2: #the key2: 20--the value2: #the key2: 24--the value2: $the key2: 31--the value2: @the key2: 36--the value2: %the key2: 37--the value2: /the key2: 41--the value2: ]AAD*DFaaaa#aaadddddd#ddf$fffffj@klss%/sss]
0 0
- 字符串排序,特殊字符位置不变
- 华为:现有一串字符串,请对字符串中的数字字符按照升序排序,其他非数字字符存储位置保持不变,将结果保存至output数组中
- 【算法逻辑】保持字符串内符号位置不变,其他字符随机变化
- 字符串重新排序,数字字符移到右边,字母字符移到左边,保持字符在原字符串中的前后顺序不变
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间。
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间
- 把一个字符串的大写字母放到字符串的后面 ,各个字符的相对位置不变,不能申请额外的空间
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间。
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间
- 对数组排序,要求相对位置不变
- vc字符串特殊字符
- 字符串特殊字符特殊处理方法:stringByTrimmingCharactersInSet
- 删除字符串中的特殊字符
- 替换字符串中的特殊字符
- 替换字符串中的特殊字符
- android 字符串特殊字符转义
- 高版本chrome安装flashplayer debuger后无法使用的问题
- Link 定制网站收藏夹图标
- mongodb基础操作
- 二维码扫描和成像,成像带小图标 自定义色值
- JVM虚拟机
- 字符串排序,特殊字符位置不变
- Fragment详解之六——如何监听fragment中的回退事件与怎样保存fragment状态
- Android入门(30)——第七章 使用WebView在界面中显示网页
- Java生成和操作Excel文件
- 我的git配置
- MySQL详解(25)-----------MySQL性能优化
- java学习12--其他流程控制结构:break和continue
- hadoop中mapreduce的常用类(一)
- Android中LayoutInflater的使用