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



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

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

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

使用以下代码:

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

最新更新