public static String getMultiChars( char c, int number ) { StringBuffer sb = new StringBuffer(); for( int i = 0; i < number; i++ ) { sb.append(c); } return sb.toString(); }
public class Test2 { public static void main(String[] args) { String str = "xy5dew_dp6n768"; char[] chs = str.toCharArray(); StringBuffer sb = new StringBuffer(); for(int i = 0; i < chs.length; i++) { sb.append(chs[i]); if((chs[i] >= '0') && (chs[i] <= '9') && (i + 1) < chs.length) { int repeat = chs[i] - '0'; char c = chs[++i]; sb.append(repeatChar(c, repeat)); } } System.out.println(sb.toString()); }
private static String repeatChar(char c, int repeat) { char[] chs = new char[repeat]; for(int i = 0; i < repeat; i++) { chs[i] = c; } return new String(chs); } }
public static String formatString(String str) { str = str.replaceAll("_", "@"); Pattern pattern = Pattern.compile("\\d[^\\d^@]"); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String group = matcher.group(); //System.out.println(group); char[] chs = new char[1]; chs[0] = group.charAt(0); int n = Integer.parseInt(new String(chs)); char[] chs2 = new char[n]; for (int i = 0; i < n; i++) { chs2[i] = group.charAt(1); } matcher.appendReplacement(sb, n + new String(chs2)); } matcher.appendTail(sb); return sb.toString(); }
/** * @param args */ public static void main(String[] args) { String numberChars = "1234567890"; String example = "xy5dew_dp6n"; int offset = 0; int copyTimes = 0; for (int i = 0; i < example.length(); i++) { if (numberChars.indexOf(example.charAt(i)) > -1) { // 计算数字偏移量 offset++; System.out.print(example.charAt(i)); continue; } else if (offset > 0) { // 计算重复次数 copyTimes = new Integer(example.substring(i - offset, i)).intValue(); } if (copyTimes > 0) { // 按照指定次数进行字符输出 for (int x = 0; x < copyTimes; x++) { System.out.print(String.valueOf(example.charAt(i))); } // 重置计数变量 copyTimes = 0; offset = 0; } else if ('_' == example.charAt(i)){ // _ 替换 @ System.out.print('@'); } else { // 以常规方法输出字符 System.out.print(example.charAt(i)); } } }
var v = 'xy5dew_dp6n768';
v = v.replace(/(\d)(.)/g, function(m, m1, m2) {
var s = [];
for(var i = 0; i < m1; i++) {
s.push(m2);
}
return m1 + s.join('');
});
alert(v);
}Java 的话,我做的就比较麻烦import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Test2 { public static void main(String[] args) {
String str = "xy5dew_dp6n768";
Pattern pattern = Pattern.compile("(\\d)(.)");
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
int repeat = Integer.parseInt(matcher.group(1));
char c = matcher.group(2).charAt(0);
matcher.appendReplacement(sb, repeat + repeatChar(c, repeat));
}
matcher.appendTail(sb);
System.out.println(sb.toString());
}
private static String repeatChar(char c, int repeat) {
char[] chs = new char[repeat];
for(int i = 0; i < repeat; i++) {
chs[i] = c;
}
return new String(chs);
}
}
要考虑的东西蛮多的
public static void main( String[] args )
{
String text = convertString("xy5dew_dp6n");
System.out.println(text);
} public static String convertString(String str)
{
StringBuffer sb = new StringBuffer(str.replaceAll("_", "@"));
String temp = "";
char[] chars = str.toCharArray();
Map<Integer, String> positionMap = new LinkedHashMap<Integer, String>();
for (int i = 0; i < chars.length; i++)
{
temp = "";
int number = -1;
try
{
number = Integer.valueOf(str.charAt(i) + "");
if( i + 1 < chars.length)
{
if( number > 0 )
{
temp = getMultiChars(chars[i+1], number);
}
positionMap.put(i+1, temp);
}
}
catch(NumberFormatException nfe)
{
continue;
}
}
if( positionMap.keySet() != null && positionMap.keySet().size() != 0 )
{
Integer[] keys = positionMap.keySet().toArray(new Integer[]{});
for( int i = keys.length - 1; i >= 0; i-- )
{
sb.insert(keys[i].intValue(), positionMap.get(keys[i].intValue()));
}
}
return sb.toString();
}
public static String getMultiChars( char c, int number )
{
StringBuffer sb = new StringBuffer();
for( int i = 0; i < number; i++ )
{
sb.append(c);
}
return sb.toString();
}
String str = "xy5dew_dp6n768";
char[] chs = str.toCharArray();
StringBuffer sb = new StringBuffer();
for(int i = 0; i < chs.length; i++) {
sb.append(chs[i]);
if((chs[i] >= '0') && (chs[i] <= '9') && (i + 1) < chs.length) {
int repeat = chs[i] - '0';
char c = chs[++i];
sb.append(repeatChar(c, repeat));
}
}
System.out.println(sb.toString());
}
private static String repeatChar(char c, int repeat) {
char[] chs = new char[repeat];
for(int i = 0; i < repeat; i++) {
chs[i] = c;
}
return new String(chs);
}
}
str = str.replaceAll("_", "@");
Pattern pattern = Pattern.compile("\\d[^\\d^@]");
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String group = matcher.group();
//System.out.println(group);
char[] chs = new char[1];
chs[0] = group.charAt(0);
int n = Integer.parseInt(new String(chs)); char[] chs2 = new char[n];
for (int i = 0; i < n; i++) {
chs2[i] = group.charAt(1);
}
matcher.appendReplacement(sb, n + new String(chs2));
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
String numberChars = "1234567890";
String example = "xy5dew_dp6n";
int offset = 0;
int copyTimes = 0;
for (int i = 0; i < example.length(); i++) {
if (numberChars.indexOf(example.charAt(i)) > -1) {
// 计算数字偏移量
offset++;
System.out.print(example.charAt(i));
continue;
} else if (offset > 0) {
// 计算重复次数
copyTimes = new Integer(example.substring(i - offset, i)).intValue();
}
if (copyTimes > 0) {
// 按照指定次数进行字符输出
for (int x = 0; x < copyTimes; x++) {
System.out.print(String.valueOf(example.charAt(i)));
}
// 重置计数变量
copyTimes = 0;
offset = 0;
} else if ('_' == example.charAt(i)){
// _ 替换 @
System.out.print('@');
} else {
// 以常规方法输出字符
System.out.print(example.charAt(i));
}
}
}
public static String expand(String s)
{
char c=0; int d=0;
StringBuilder sb=new StringBuilder();
for(int i=0;i<s.length();i++)
{
sb.append((c=s.charAt(i))=='_'?'@':c );
for(char c1='1';Character.isDigit(c) && (i+1<s.length()) && (c1<=c);c1++)
{
sb.append(s.charAt(i+1));
}
if(Character.isDigit(c)) i++;
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println("xy5dew_dp6n768=>"+expand("xy5dew_dp6n768"));
}
}程序运行结果:
xy5dew_dp6n768=>xy5dddddew@dp6nnnnnn766666668
xy5dew_dp6n768=>xy5dddddew@dp6nnnnnn766666668最后一个8是不是应该重复6次呢?
public static void main(String args[])
{
String s = "safrw32d2saf_fsdf@ggsaf545wq1esafs";
s = s.replace("_", "@");
StringBuffer sb = new StringBuffer();
StringBuffer num = new StringBuffer();
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(c>'0' && c<'9'){
num.append(c);
}else{
if(num.length()<=0)
sb.append(c);
else{
int j = Integer.valueOf(num.toString());
for(int x=0;x<j;x++){
sb.append(c);
}
num = new StringBuffer();
}
}
}
System.out.println(sb.toString());
}
{
String s = "saf_fsdf@g5wq1esafs11";
s = s.replace("_", "@");
StringBuffer sb = new StringBuffer();
StringBuffer num = new StringBuffer();
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(c>'0' && c<'9'){
num.append(c);
if(i==s.length()-1){
sb.append(num);
}
}else{
if(num.length()<=0)
sb.append(c);
else{
int j = Integer.valueOf(num.toString());
for(int x=0;x<j;x++){
sb.append(c);
}
num = new StringBuffer();
}
}
}
System.out.println(sb.toString());
}
{
public static void main(String[] args)
{
String s="xy5dew_dp6n768";
StringBuffer sb=new StringBuffer();
for(int i=0;i<s.length();i++)
{
if(Character.isDigit(s.charAt(i)))
{
sb.append(s.charAt(i));
for(char j='0';j<s.charAt(i)&&(i+1)<s.length();j++)
{
sb.append(s.charAt(i+1));
}
i++;
}
else if(s.charAt(i)=='_')
{
sb.append('@');
}
else
sb.append(s.charAt(i));
}
System.out.println(sb);
}
}
public static String did(String s)
{ String after="";
for(int i=0;i<s.length();i++)
{
int a=(int)s.charAt(i);
if(a>=48&&a<=57&&i<s.length()-1)
{ after=after+ s.charAt(i);
for(int j=0;j<a-49;j++)
after=after+s.charAt(i+1);
}
else if(a==95)
{
after=after+'@';
}
else
{
after=after+ s.charAt(i);
}
}
return after;
}//did()函数返回处理后的结果