从逗号sep属性列表创建数组的更优雅的解决方案?

时间:2022-09-25 10:06:27

In my properties file I have one property, which contains a comma separated list of values

在我的属性文件中,我有一个属性,其中包含逗号分隔的值列表

In my code, I would like to load that property in, split it from the commas, and add each value into an array. I also want to make sure I don't have values in the array due to whitespace etc

在我的代码中,我想加载该属性,将其从逗号中拆分,并将每个值添加到数组中。我还想确保由于空格等原因我没有数组中的值

example property :

示例属性:

prop_allowed_extensions = .jpeg,tiff, .txt 

I have come up with this so far, but it feels dirty, is there a more elegant solution?

到目前为止,我已经想出了这个,但它感觉很脏,是否有更优雅的解决方案?

String test = classProperties.getProperty("prop_allowed_extensions", "txt, jpeg");
String[] splitString = StringUtils.split(test, ',');
String[] newString = new String[splitString.length];             
for (int i = 0; i < splitString.length; i++)
{
   newString[i] = StringUtils.trim(splitString[i]);                
}

I just want the text of the file extension, would it be appropriate to use a regular expression to remove whitespace/non-letters?

我只想要文件扩展名的文本,是否适合使用正则表达式删除空格/非字母?

7 个解决方案

#1


4  

You can do it in two steps:

您可以分两步完成:

String[] allowedExtensions = str.replaceAll("[^\\w,_]", "").split(",")

(First kill anything that is not either a comma, an underscore or a word character (letter or digit), then split the resulting string

(首先杀死任何不是逗号,下划线或单词字符(字母或数字)的东西,然后拆分结果字符串

#2


7  

Apache Jakarta Commons Configuration handles properties file in a clean way, allowing you to specify lists in different formats:

Apache Jakarta Commons Configuration以干净的方式处理属性文件,允许您以不同的格式指定列表:

colors.pie = #FF0000, #00FF00, #0000FF

OR

要么

colors.pie = #FF0000;
colors.pie = #00FF00;
colors.pie = #0000FF;

And get the list like this:

并得到这样的列表:

String[] colors = config.getStringArray("colors.pie");
List colorList = config.getList("colors.pie");

See the How-to

请参阅操作方法

#3


4  

I'd personally just reduce it to the following (keeping in mind that String.split() takes a regular expression), but I don't know whether or not you'd consider it more elegant:

我个人只是将它减少到以下(记住String.split()采用正则表达式),但我不知道你是否认为它更优雅:

String test = classProperties.getProperty("prop_allowed_extensions",
        "txt, jpeg");
String[] extensions = test.trim().split("\\s*,\\s*");

Edit: You can remove the leading dot on each extension too, if it is present (this, or one of several other ways):

编辑:您也可以删除每个扩展名上的前导点,如果它存在(这或其他几种方式之一):

String[] test = classProperties.getProperty("prop_allowed_extensions",
        "txt, jpeg").trim().split("^\\.", 2);
String[] extensions = test[test.length - 1].split("\\s*,\\s*.?");

#4


1  

Following your pattern, you can do it like this:

按照你的模式,你可以这样做:

String test = classProperties.getProperty("prop_allowed_extensions", "txt, jpeg");
List<String> extensions = new ArrayList<String>();         
for (String part:test.split(",")) {
   extensions.add(part.trim());               
}

The difference is that the result is in a list now. test.split(",") will be called only once, so there is no performance issue.

不同之处在于结果现在在列表中。 test.split(“,”)只会调用一次,因此没有性能问题。

#5


1  

String[] allowedFormats = classProperties
                           .getProperty("prop_allowed_extensions", "txt, jpeg")
                           .replaceAll("[ \\.]","")
                           .split(",");

With Java 1.5 or superior, in Array format.

使用Java 1.5或更高版本,采用Array格式。

#6


0  

It's much more simple:

它更简单:

String test = classProperties.getProperty("prop_allowed_extensions", "txt, jpeg");
String[] splitString = test.trim().split("\\s*,\\s*");

(needs Java 5)

(需要Java 5)

#7


-1  

String test = classProperties.getProperty("prop_allowed_extensions", "txt, jpeg");
String[] splitString = StringUtils.split(test, ',');
for(String s : splitString) {
    s = StringUtils.trim(s);
}

would be slightly more elegant, but Java does not support LINQ like .net where you would be able to avoid the explicit loop altogether.

稍微优雅,但Java不支持像.net这样的LINQ,你可以完全避免显式循环。

#1


4  

You can do it in two steps:

您可以分两步完成:

String[] allowedExtensions = str.replaceAll("[^\\w,_]", "").split(",")

(First kill anything that is not either a comma, an underscore or a word character (letter or digit), then split the resulting string

(首先杀死任何不是逗号,下划线或单词字符(字母或数字)的东西,然后拆分结果字符串

#2


7  

Apache Jakarta Commons Configuration handles properties file in a clean way, allowing you to specify lists in different formats:

Apache Jakarta Commons Configuration以干净的方式处理属性文件,允许您以不同的格式指定列表:

colors.pie = #FF0000, #00FF00, #0000FF

OR

要么

colors.pie = #FF0000;
colors.pie = #00FF00;
colors.pie = #0000FF;

And get the list like this:

并得到这样的列表:

String[] colors = config.getStringArray("colors.pie");
List colorList = config.getList("colors.pie");

See the How-to

请参阅操作方法

#3


4  

I'd personally just reduce it to the following (keeping in mind that String.split() takes a regular expression), but I don't know whether or not you'd consider it more elegant:

我个人只是将它减少到以下(记住String.split()采用正则表达式),但我不知道你是否认为它更优雅:

String test = classProperties.getProperty("prop_allowed_extensions",
        "txt, jpeg");
String[] extensions = test.trim().split("\\s*,\\s*");

Edit: You can remove the leading dot on each extension too, if it is present (this, or one of several other ways):

编辑:您也可以删除每个扩展名上的前导点,如果它存在(这或其他几种方式之一):

String[] test = classProperties.getProperty("prop_allowed_extensions",
        "txt, jpeg").trim().split("^\\.", 2);
String[] extensions = test[test.length - 1].split("\\s*,\\s*.?");

#4


1  

Following your pattern, you can do it like this:

按照你的模式,你可以这样做:

String test = classProperties.getProperty("prop_allowed_extensions", "txt, jpeg");
List<String> extensions = new ArrayList<String>();         
for (String part:test.split(",")) {
   extensions.add(part.trim());               
}

The difference is that the result is in a list now. test.split(",") will be called only once, so there is no performance issue.

不同之处在于结果现在在列表中。 test.split(“,”)只会调用一次,因此没有性能问题。

#5


1  

String[] allowedFormats = classProperties
                           .getProperty("prop_allowed_extensions", "txt, jpeg")
                           .replaceAll("[ \\.]","")
                           .split(",");

With Java 1.5 or superior, in Array format.

使用Java 1.5或更高版本,采用Array格式。

#6


0  

It's much more simple:

它更简单:

String test = classProperties.getProperty("prop_allowed_extensions", "txt, jpeg");
String[] splitString = test.trim().split("\\s*,\\s*");

(needs Java 5)

(需要Java 5)

#7


-1  

String test = classProperties.getProperty("prop_allowed_extensions", "txt, jpeg");
String[] splitString = StringUtils.split(test, ',');
for(String s : splitString) {
    s = StringUtils.trim(s);
}

would be slightly more elegant, but Java does not support LINQ like .net where you would be able to avoid the explicit loop altogether.

稍微优雅,但Java不支持像.net这样的LINQ,你可以完全避免显式循环。