(1)若当前字符不是大于0的数字字符,则复制该字符于新字符串中; (2)若当前字符是一个数字字符,且它之后没有后继字符,则简单地将它复制到新字符串中; (3)若当前字符是一个大于0的数字字符,并且还有后继字符,设该数字字符的面值为n,将它的后继字符重复复制n+1次到新字符串中; (4)以上述一次变换为一组,在不同组之间另插入一个"_"用于分割(5))若字符串中包含有下划线'_',则变换为 UL

package b;

import java.util.Scanner;

public class Zifuchuan {

    public static void main(String[] args) {
        System.out.println("输入字符串:");
        Scanner zifu=new Scanner(System.in);
        String s=zifu.nextLine();
        String shuzi="123456789";
        for (int i = 0; i < s.length(); i++) 
        {
            if(shuzi.replace(s.substring(i, i+1), "").length()==(shuzi.length()-1)
                    &&i<s.length()-1)
            {
                int n=new Integer(s.substring(i, i+1));
                for (int j = 0; j <n+1; j++) 
                {
                     System.out.print(s.substring(i+1,i+2));                    
                }
                System.out.print("_");
            }
            else if(shuzi.replace(s.substring(i, i+1), "").length()==(shuzi.length()-1)
                    &&i==s.length()-1)
            {                
                
                System.out.print(s.substring(i,i+1));            
                                
            }
            else if(s.substring(i, i+1).equals("_")&&i<s.length()-1)
            {
                System.out.print("\UL");
                System.out.print("_");
            }
            else if(s.substring(i, i+1).equals("_")&&i==s.length()-1)
            {
                System.out.print("\UL");
            }
            else if(i<s.length()-1)
            {
                System.out.print(s.substring(i, i+1));
                System.out.print("_");
            }
            else
            {
                System.out.print(s.substring(i, i+1));        
            }
        }
    }

}

原文地址:https://www.cnblogs.com/wenwen123/p/5520338.html