【发布时间】:2020-06-07 20:44:08
【问题描述】:
对于我正在编写的工具,我需要一个“随机文本”生成器。我想让用户能够从这些预制字符串中进行选择:
const string baseCollection = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const string numbers = "0123456789";
const string specialChars = "°^!\"§$%&/{([)]=}?\\`´*+~'#,;.:-_><|";
const string germanAddition = "ÄÖÜäöü";
const string frenchAddition = "éàèùâêîôûäëïü眫»";
const string russianAddition = " бвгджзклмнпрстфхцчшщйаэыуояеёюиьъ";
应该使用哪些术语来运行此方法。
public string RanText(int length, ???)
{
string charCollectionString = "";
foreach(string str in charCollectionStrings) {
charCollectionString += str;
}
//stuff
return finalString;
}
我曾想过使用枚举,但那些不允许使用字符串。创建一系列可能参数的最简洁方法是什么?
【问题讨论】:
-
枚举似乎是这里的答案。你和他们有什么问题?你希望调用者能够传递多个选择吗?例如
baseCollection和numbers -
您可以编写一个
if else块来检查他们选择了哪些选项,然后在每个块中连接一个字符串。else if(choice4) { masterString += germanAddition },然后将该字符串作为charCollectionString传递给您的RanText方法。 -
@Sweeper 是的!这正是我想要的。例如:用户创建:new TestHelper() -> 为具有两个字符集的随机文本调用 TestHelper.RanText(15, baseCollection, German)
-
签出this。
-
我已经看到了将 Enum 与 IfElse 或 Switch 一起使用的可能性,但随着时间的推移,这不会变得非常混乱吗?就像想象有一天会有 30 多个可能的字符集.. 必须有一种更清洁的方式(我希望)