/*
* 文 件 名: StringCombin.java
* 版 权:
* 描 述: <描述>
* 修 改 人: hpli/025415
* 修改时间: 2011-6-11
* 跟踪单号: <跟踪单号>
* 修改单号: <修改单号>
* 修改内容: <修改内容>
*/
package com.util;
/**
* 去掉重复字符串 例如12342343454565678789->123456789
*
*
* @author hpli/025415
* @version [版本号, 2011-6-11]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class StringCombin
{
public static String fatherString = "";
public static String getString(String str)
{
String stmp = "";
int i;
c: for (i = fatherString.length(); i < str.length(); i++)
{
if (fatherString.contains(String.valueOf(str.charAt(i))))
{
int origin = i;
int j;
if (i == str.length() - 1)
{
String tempString = str.substring(str.length() - 2);
if (!fatherString.contains(tempString))
fatherString += tempString;
break;
}
else if (i == str.length())
{
fatherString += str.substring(str.length() - 1);
break;
}
for (j = origin + 1; j < str.length(); j++)
{
String innString = str.substring(origin, j);
if (fatherString.contains(innString))
{
}
else
{
if (j - origin > 2)
{
StringBuffer sb = new StringBuffer(str);
stmp = sb.delete(origin, j - 1).toString();
fatherString = "";
break c;
}
else
{
stmp = str;
fatherString = fatherString
+ str.substring(origin, j - 1);
break;
}
}
}
}
else
{
fatherString = fatherString + String.valueOf(str.charAt(i));
}
}
if (i == str.length())
{
return fatherString;
}
System.out.println(stmp + "_" + fatherString);
return getString(stmp);
}
public static void main(String[] args)
{
String str = "12342343454565678789a";
System.err.println(getString(str));
}
}
方案二:
/**
* 去掉重复字符串并排序 例如12342343454565678789->123456789
*
* @author hpli/025415
* @version [版本号, 2012-5-28]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class StringCombin{
public static String temp = "";
public static List<String> list = new ArrayList<String>();
public static void getStr(String str) {
for (int i = 0; i < str.length(); i++) {
if (!temp.contains(String.valueOf(str.charAt(i)))) {
temp += String.valueOf(str.charAt(i));
list.add(String.valueOf(str.charAt(i)));
}
}
Collections.sort(list);
}
public static void main(String[] args) {
String str = "12933215488485452151adfjksadfkcccvcdsfsdfsdfsd";
getStr(str);
String temp = "";
for (int i = 0; i < list.size(); i++) {
temp += list.get(i);
}
System.err.println(temp);
}
}