我有以下字符串
String S1="S1_T1_VIEW";
我希望它被拆分并分配给这样的字符串:
String permission = "VIEW";
String component = "S1_T1";
String parent = "S1";
我尝试使用 S1.split()
函数,但没有太大帮助。
字符串也可以这样
String S1="S1_T1_C1_DELETE";
那个时候结果应该是
String permission = "DELETE";
String component = "S1_T1_C1";
String parent = "S1_T1";
任何建议都会有所帮助。
提前致谢
最佳答案
我假设如下:
permission
是S1
最后一个下划线之后的部分。component
是S1
最后一个下划线之前的部分。parent
是component
中 its 最后一个下划线之前的部分。
如果是这样,也许试试下面的方法?这本质上只是对上述规则的字面解释,通过找到合适的下划线来拆分字符串。
int lastUnderscore = S1.lastIndexOf("_");
String permission = S1.substring(lastUnderscore + 1);
String component = S1.substring(0, lastUnderscore);
lastUnderscore = component.lastIndexof("_");
String parent = component.substring(0, lastUnderscore);
关于java - 拆分字符串 ('_' 作为分隔符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16603425/