This question already has answers here:
How to split a string in Java
                                
                                    (34个答案)
                                
                        
                4年前关闭。
            
        

我有一个定界字符串,定界符为“ $%”。字段按特定顺序排列。例如:John $%Smith $%30 $%Los Angeles
我需要获取此字符串的值并将其存储在各自的属性中

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/

10-11 04:04