Java 按句点拆分字符串

Java 按句点拆分字符串,java,Java,您好,我有以下字符串,我正试图拆分为hibernate createAlias和查询限制 我需要把绳子分成三部分 employeeProfile.userProfile.shortname 1. employeeProfile.userProfile 2. userProfile 3. userProfile.shortName 1. employeeProfile.userProfile.anotherClass 2. userProfile.anotherClass 3. anotherC

您好,我有以下字符串,我正试图拆分为hibernate createAlias和查询限制

我需要把绳子分成三部分

employeeProfile.userProfile.shortname

1. employeeProfile.userProfile
2. userProfile
3. userProfile.shortName
1. employeeProfile.userProfile.anotherClass
2. userProfile.anotherClass
3. anotherClass.shortName
我还希望它是动态的,可以生成不同长度的字符串

employeeProfile.userProfile.anotherClass.shortname

1. employeeProfile.userProfile
2. userProfile
3. userProfile.shortName
1. employeeProfile.userProfile.anotherClass
2. userProfile.anotherClass
3. anotherClass.shortName
使用以下代码,除了第三个代码外,我能够让大部分代码正常工作

public void hasAlias(Criteria t, final Map<String, Object> map) {
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        String key = entry.getKey();
        if (key != null && key.contains(".")) {
            key = key.substring(0, key.lastIndexOf("."));
            String value = key.contains(".") ? key.substring(key.lastIndexOf(".") + 1, key.length()) : key;
            t.createAlias(key, value);
        }
    }
}
public void hasAlias(标准t,最终地图){
对于(Map.Entry:Map.entrySet()){
String key=entry.getKey();
if(key!=null&&key.contains(“.”){
key=key.substring(0,key.lastIndexOf(“.”);
字符串值=key.contains(“.”)key.substring(key.lastIndexOf(“.”+1,key.length()):key;
t、 createAlias(键、值);
}
}
}
有人能帮我弄到3号吗

employeeProfile.userProfile.shortname

  • employeeProfile.userProfile
  • 用户配置文件
  • userProfile.shortName
  • 假设我们有:

    int index1 = str.indexOf(".");
    int index2 = str.lastIndexOf(".");
    
    然后这就起作用了(模块+1在这里和那里):

  • 子串(0,index2)
  • 子串(index1,index2)
  • 子串(index1)
  • employeeProfile.userProfile.shortname

  • employeeProfile.userProfile
  • 用户配置文件
  • userProfile.shortName
  • 假设我们有:

    int index1 = str.indexOf(".");
    int index2 = str.lastIndexOf(".");
    
    然后这就起作用了(模块+1在这里和那里):

  • 子串(0,index2)
  • 子串(index1,index2)
  • 子串(index1)
  • 看一看。例如,您可以执行以下操作:

    String[] tmp="foo.bar".split("."); 
    
    一旦你把它做成这种形式,你就可以用它做任何你需要做的事情。

    看看。例如,您可以执行以下操作:

    String[] tmp="foo.bar".split("."); 
    

    一旦你把它变成这种形式,你就可以用它做任何你需要做的事情。

    要得到数字3,你可以使用正则表达式。接受最后两项的正则表达式将是:

    [a-zA-z]+\.[a-zA-z]+$
    
    使用以下代码获取数字3:

    Pattern pattern = Pattern.compile("[a-zA-z]+\\.[a-zA-z]+$");
    Matcher matcher = p.matcher("employeeProfile.userProfile.anotherClass.shortname");
    
    if (m.find()) {
        System.out.println(m.group(1));
    }
    
    这将打印:

    anotherClass.shortname
    

    要获得数字3,可以使用正则表达式。接受最后两项的正则表达式将是:

    [a-zA-z]+\.[a-zA-z]+$
    
    使用以下代码获取数字3:

    Pattern pattern = Pattern.compile("[a-zA-z]+\\.[a-zA-z]+$");
    Matcher matcher = p.matcher("employeeProfile.userProfile.anotherClass.shortname");
    
    if (m.find()) {
        System.out.println(m.group(1));
    }
    
    这将打印:

    anotherClass.shortname
    

    我认为使用Java的StringTokenizer可能会更幸运:

    您可以这样设置:

    String myString = "employeeProfile.userProfile.shortname";
    String myDelimiter = ".";
    StringTokenizer tokens = new StringTokenizer(myString,myDelimiter);
    

    由此,您应该能够获得所需的并使用令牌。

    我认为您可能会更幸运地使用Java的StringTokenizer:

    您可以这样设置:

    String myString = "employeeProfile.userProfile.shortname";
    String myDelimiter = ".";
    StringTokenizer tokens = new StringTokenizer(myString,myDelimiter);
    

    由此,您应该能够获得所需的并使用代币。

    第3个索引在哪里?应该是
    index1
    。更正。@George第二个示例不适用于此。数字3将不正确。正确。在我回答后又加上了。。。我也不明白第二个的逻辑example@Summerbulb您是对的,它似乎在快速测试中起作用,但当我实现它时,它不起作用。我忘了调整答案。第3号的索引在哪里?应该是
    index1
    。更正。@George第二个示例不适用于此。数字3将不正确。正确。在我回答后又加上了。。。我也不明白第二个的逻辑example@Summerbulb您是对的,它似乎在快速测试中起作用,但当我实现它时,它不起作用。我忘了调整答案。