简体   繁体   English

需要从字符串中提取 3 个值

[英]Need to extract 3 values from string

I receive values like this from database table.我从数据库表中收到这样的值。

String DBValue = "AssessmentID-289_ApplicationName-OneCert_AccountName-9000032"字符串 DBValue = "AssessmentID-289_ApplicationName-OneCert_AccountName-9000032"

From this string I want to extract the values of AssessmentID, ApplicationName and AccountName and store it in these String variables "Assessment_ID","Application_Name" and "AccountName".我想从这个字符串中提取AssessmentID、ApplicationName 和AccountName 的值并将其存储在这些字符串变量“Assessment_ID”、“Application_Name”和“AccountName”中。

Note: I won't be able to do any changes in database.注意:我将无法对数据库进行任何更改。

String#split

Call String#split multiple times.多次调用String#split

  1. First split on the LOW LINE (underscore) character _ to get the three fields.首先拆分 LOW LINE(下划线)字符_以获得三个字段。
  2. Then on each of those fields, call split on the HYPHEN-MINUS character - to split into two parts: field name and field value.然后在每个字段上,对 HYPHEN-MINUS 字符调用split -拆分为两部分:字段名称和字段值。
String dbValue = "AssessmentID-289_ApplicationName-OneCert_AccountName-9000032" ;
String[] fields = dbValue.split( "_" ) ;  // Produces "AssessmentID-289", "ApplicationName-OneCert", and "AccountName-9000032".
Map< String , String > results = new LinkedHashMap<>() ;
for( String field : fields )
{
    String[] parts = field.split( "-" ) ;   // Produces pairs "AssessmentID" & "289", "ApplicationName" & "OneCert", and "AccountName" & "9000032".
    if(parts.length >= 2) {
        results.put( parts[0] , parts[1] ) ;  // key is field name, value is field value.
    }
}

See this code run live at IdeOne.com .请参阅在 IdeOne.com 上实时运行的代码

results.toString(): {AssessmentID=289, ApplicationName=OneCert, AccountName=9000032} results.toString(): {AssessmentID=289, ApplicationName=OneCert, AccountName=9000032}

If you make a method to perform this chore, I would suggest returning an unmodifiable map .如果您制定了一种方法来执行这项工作,我建议您返回一个不可修改的 map Call Map.copyOf .致电Map.copyOf

return Map.copyOf( results ) ;  // Return an unmodifiable map.

Better yet, in Java 16 and later, return a record .更好的是,在 Java 16 及更高版本中,返回一条记录 A record is a brief way to write a class whose main purpose is to transparently and immutably communicate data.记录是编写 class 的简单方法,其主要目的是透明且不可变地通信数据。 The compiler implicitly creates the constructor, getters, equals & hashCode , and toString .编译器隐式创建构造函数、getter、 equals & hashCodetoString

public record Assessment( String id , String applicationName , String accountName ) {}

Return an object of that type.返回该类型的 object。

return new Assessment(
    results.get( "AssessmentID" ) ,
    results.get( "ApplicationName" ) ,
    results.get( "AccountName" ) 
) ;

In real work, I would throw in some calls to Objects.requireNonNull , verify the expected size of the map, and perform some other data validation.在实际工作中,我会调用Objects.requireNonNull ,验证 map 的预期大小,并执行一些其他数据验证。


Sounds like the source database was badly designed, burying multiple values within one field.听起来源数据库设计得很糟糕,在一个字段中隐藏了多个值。 Consider educating the responsible person about database normalization .考虑对负责人进行有关数据库规范化的教育。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM