String去除重复字符两个方法
package cn.aresoft;
import java.util.ArrayList;
import java.util.List;
public class TestBasic {
public static void main(String[] args) {
String str = "kjkljklhuionsd";
System.out.println(sub(str));
System.out.println(sub1(str));
}
// 方法一:
static String sub(String str) {
StringBuffer result = new StringBuffer();
List list = new ArrayList();
char[] cs = str.toCharArray();
for (int i = 0; i < cs.length; i++) {
if (!list.contains(cs[i])) {
result.append(cs[i]);
list.add(cs[i]);
}
}
return result.toString();
}
// 方法二:
static String sub1(String str) {
List list = new ArrayList();
StringBuffer sb = new StringBuffer(str);
int j = 0;
for (int i = 0; i < str.length(); i++) {
if (list.contains(str.charAt(i))) {
sb.deleteCharAt(i - j); // String 是没有delete方法的
j++; // 因为删除了sb中的字符,有一个偏移
} else {
list.add(str.charAt(i));
}
}
return sb.toString();
}
}
String去除重复字符两个方法的更多相关文章
- string [] 去除重复字符两个方法
不废话直接看图 结果 代码: this.txtListHTML.Text = String.Join(",", list.Replace("\r\n", &qu ...
- Money类型转化为String去除小数点后0解决方法
Money类型转化为String去除小数点后0从数据库提取Money类型后,字符串如:1212.0000 如何使其成为1212 注:去掉了小数点 如果是:1212.0100 使 ...
- ArrayList去除重复元素(多种方法实现)
package other; import java.util.ArrayList; import java.util.HashSet; public class test4 { public sta ...
- [LeetCode] Repeated String Match 重复字符串匹配
Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...
- Javascript一句代码实现JS字符串去除重复字符
需求: 原字符串:abcdabecd 去重后字符串:abcde JS字符串去重,一个简单需求,网上找案例发现都是一大堆代码,对于强迫症的我 实再无法忍受,于是自己手动写出一段代码,完美解决该问题. 代 ...
- C# char[]与string互相转换的两种方法
1.string转换为char[]:char[] string.ToCharArray(); static void Main(string[] args) { s ...
- Java中List集合去除重复数据的六种方法
1. 循环list中的所有元素然后删除重复 public static List removeDuplicate(List list) { for ( int i = 0 ; i < list. ...
- [LeetCode] 686. Repeated String Match 重复字符串匹配
Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...
- sql去除重复的几种方法
所以用这样一句SQL就可以去掉重复项了: select * from msg group by terminal_id; SQL中distinct的用法(四种示例分析) 示例1 select dist ...
随机推荐
- 复习--二叉树&&树
树是一种很常用的数据结构,日后的学习中会经常碰到运用树的知识. //构造二叉树#include<cstdio> #include<iostream> #include<a ...
- OSGI依赖问题处理
用osgi实现java的模块化和热插拔时要考虑好两个问题,不同bundle间如何通信?依赖怎么处理? OSGi的一个标准就是各个bundle之间是相互隔离的,每个bundle都有自己的classloa ...
- .NET页面事件执行顺序
摘自:http://www.cnblogs.com/kenkofox/archive/2011/03/18/1987998.html和http://blog.csdn.net/yiruoyun/art ...
- google搜索引擎使用方法
搜索引擎命令大全!这是一个我最喜欢的Google搜索技巧的清单: link:URL = 列出到链接到目标URL的网页清单. related:URL = 列出于目标URL地址有关的网页. site:ht ...
- 第3课 把文件存入Git文档库
3-1 排除不需要加入文档库的文件 Git追踪文件的方式.Git会将文件和文件夹分成以下三类: 1. 被追踪的(tracked): 2. 忽略的(ignored): 3. 不被追踪的(u ...
- Python入门 来点栗子
查天气(1) http://wthrcdn.etouch.cn/weather_mini?citykey=101280804 http://wthrcdn.etouch.cn/WeatherApi?c ...
- Help Me with the Game(模拟)
http://poj.org/problem?id=2996 #include<stdio.h> #include<string.h> ][]; void find1(char ...
- 如何让 vue 在 sublime 中变成彩色的
在 sublime 中编辑 vue 时,导入后是纯白色的文本,如下图: 想让其变成彩色的文本,需要安装插件,步骤如下: 1. 按住:Ctrl + Alt + P 2. 输入:install Packa ...
- asp.net限制了上传文件大小为..M,解决方法
asp.net限制了上传文件大小为4M,在:在web.config里加下面一句,加在<System.web></System.web>之间如下:<system.web&g ...
- checked、disabled在原生、jquery、vue下不同写法
以下是原生和jquery <!DOCTYPE html> <html> <head> <meta http-equiv="Content ...