Java中的处理表达式字符串[toLower,toUpper和concat]

时间:2019-07-16 09:29:22

标签: java regex string combinations permutation

我有一个toLowertoUpperconcat函数的任意组合的正则表达式。 inputString = concat(toLower(concat("ABC","xyz")),toUpper("pqr"))outputString = abcxyzPQR

表达式输入字符串可以具有三个功能[toLowertoUpperconcat]的许多组合。

如何用Java中上述3个函数的任意随机组合来处理字符串?

1 个答案:

答案 0 :(得分:0)

使用以下代码:

import java.util.Random;

    public class RandomCombination
    {

    public static void main(String args [])
    {
    Random randomGenerator=new Random(); // We will randomize between 1 to 3. We are adding in the random coz next(3) will randomize from 0-2
     String outputString= randomize("ABC",randomGenerator.nextInt(3)+1).concat(randomize("xyz",randomGenerator.nextInt(3)+1)).concat(randomize("pqr",randomGenerator.nextInt(3)+1));

     System.out.println(outputString);
    }
    static String randomize(String inputString, int ran )
    {
    String outuput;
    switch(ran){
        case 1:
        outuput=inputString.toUpperCase(); // if 1 use upper
        break;
        case 2:
        outuput= inputString.toLowerCase();  // if 2 use lower
        break;  
        default:
        outuput=inputString;  // if 3 don't do a thing
        }

        return outuput;
    }
    }

Sample output:
abcXYZPQR
abcxyzpqr
ABCXYZpqr
abcxyzPQR
abcXYZPQR