正在寻找一种优雅的模式来获取字符串的最左端,使用Java
我想从伦敦回来 我是这样做的:正在寻找一种优雅的模式来获取字符串的最左端,使用Java,java,string,Java,String,我想从伦敦回来 我是这样做的: String str="https://rumpelstiltskin.com:7071/service/admin/soap"; String strOut=""; strOut=str.split("/soap")[0]; System.out.println(strOut); 有更优雅的方法吗?使用以下方法如何: 尝试并测试输出: https://rumpelstiltskin.com:7071/service/admin 使用以下选项如何: 尝试并测试
String str="https://rumpelstiltskin.com:7071/service/admin/soap";
String strOut="";
strOut=str.split("/soap")[0];
System.out.println(strOut);
有更优雅的方法吗?使用以下方法如何: 尝试并测试输出:
https://rumpelstiltskin.com:7071/service/admin
使用以下选项如何: 尝试并测试输出:
https://rumpelstiltskin.com:7071/service/admin
你可以试试这个:-
String str = "https://rumpelstiltskin.com:7071/service/admin/soap";
String s = str.replace("/soap", "");
你可以试试这个:-
String str = "https://rumpelstiltskin.com:7071/service/admin/soap";
String s = str.replace("/soap", "");
试试这个
String str="https://rumpelstiltskin.com:7071/service/admin/soap";
String newStr = str.substring(0,str.lastIndexOf("/"));
System.out.println(newStr);
试试这个
String str="https://rumpelstiltskin.com:7071/service/admin/soap";
String newStr = str.substring(0,str.lastIndexOf("/"));
System.out.println(newStr);
仅当最后一块始终是/soap时才有效
String str="https://rumpelstiltskin.com:7071/service/admin/soap";
String strOut=str.substring(0, str.lastIndexOf("/soap"));
System.out.println(strOut);
只有在最后一块总是/soap的情况下才有效,你可以使用substringint,int,但我不知道它是否更优雅。最后一条路径总是soap,小写?你可以使用substringint,int,但我不知道它是否更优雅。最后一条路径总是soap,小写?这将是我的答案,但如果它想在url路径上检查soap,它不会回答我的评论这将是我的答案,但如果它想在url路径上检查soap,它不会回答我的评论是的。关于OP的问题,我应该更清楚我的意思。是的,会的。关于OP的问题,我应该更清楚我的意思。
String str="https://rumpelstiltskin.com:7071/service/admin/soap";
String strOut=str.substring(0, str.lastIndexOf("/soap"));
System.out.println(strOut);