java - 如何从分隔字符串中获取值?

标签 java string split

我有一个分隔字符串,分隔符是“$%”。这些字段按特定顺序排列。例如:约翰$%史密斯$%30$%洛杉矶 我需要获取该字符串的值并将它们存储在相应的属性中

Customer.firstName(_)
Customer.lastName(_)
Customer.age(_)
Customer.city(_)   

到目前为止,我已经尝试过这个,但我知道我做错了:

if(thisString != null){
        if(thisString.endsWith("$%")){
            Customer.firstName(thisString.substring(0,indexOf("$%");
        }
    }

最佳答案

尝试使用 Java 的 String.split

例如:

//Split string based on "$%"
String[] values = thisString.split(Pattern.quote("$%"));
Customer.firstName() = values[0]; //Set first name equal to first string from the split
//etc

关于java - 如何从分隔字符串中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31837986/

相关文章:

java - 在 Java 中用数据声明 HashSet 的最清晰方法

java - 在Java中制作一条数轴来显示一个点,getWidth()和getHeight()没有正确校准位置

java - 奇怪的 SQLException - 无法检索事务只读状态服务器

javascript - 如何获取字符串中最后一个字母字符?

java - 如何返回超过 1 个值的字符串?

linux - 尝试在 bash 中分割一个大字符串

python - pandas str.split给我一个意想不到的语法错误

java - 确保方法在 x 秒内每个输入仅调用一次

string - 根据规则生成字符串的变体

Java:将ResultSet拆分为多个数组(基于时间戳)