如何在java中第二次出现点(.)后拆分字符串
我有一个字符串,看起来像这样(最基本的形式):如何在java中第二次出现点(.)后拆分字符串,java,string,Java,String,我有一个字符串,看起来像这样(最基本的形式): String str=“1.0.0.190” str也可以是这样的: 1.11.0.12或2.111.1.190或1.0.0.0 我想在第二个点(.)处拆分字符串。我怎样才能做到这一点 输出: String str = "1.0.0.190" String output = "1.0" 对于其他字段: String[] halfs = str.split("\\."); String[] fulls = new String[halfs.len
String str=“1.0.0.190”
str也可以是这样的:
1.11.0.12或2.111.1.190或1.0.0.0
我想在第二个点(.)处拆分字符串。我怎样才能做到这一点
输出:
String str = "1.0.0.190"
String output = "1.0"
对于其他字段:
String[] halfs = str.split("\\.");
String[] fulls = new String[halfs.length / 2];
for (int i = 0; i < fulls.length; ++i) {
fulls[i] = halfs[2*i] + "." + halfs[2*i + 1];
}
return fulls[0];
简单地说:
return str.replaceAll("^([^.]*\\.[^.]*)\\..*$", "$1");
在具有子字符串和indexOf的一行中:
String output = str.substring(0,str.indexOf(".",str.indexOf(".")+1));
publicstaticvoidmain(字符串[]args){
字符串输入=“2.111.1.190”;
String[]out=input.split(“\\”);
字符串output1=out[0]+““+out[1];
系统输出打印项次(输出1);
字符串输出2=“”;
对于(int x=2;x;
系统输出打印项次(输出2);
}
我会根据OP的级别给出答案,所以我不会向他推荐split或regexps
如果您需要子字符串到第二个点,只需找到第二个点并将字符串剪切到该位置
public class DotSubstring {
public static void main(String[] args) {
String s = "1.2.3.4";
int secondDotPosition = findSecondDotPosition(s);
if (secondDotPosition > 0) {
System.out.println(s.substring(0, secondDotPosition));
} else {
System.out.printf("ERROR: there is not a 2nd dot in '%s'%n", s);
}
}
private static int findSecondDotPosition(String s) {
int result = -1;
int dotsToFind = 2;
char[] ca = s.toCharArray();
for (int i = 0; i < ca.length; ++i) {
if (ca[i] == '.') --dotsToFind;
if (dotsToFind == 0) return i;
}
return result;
}
}
公共类点子字符串{
公共静态void main(字符串[]args){
字符串s=“1.2.3.4”;
int secondDotPosition=findSecondDotPosition;
如果(第二个点位置>0){
System.out.println(s.substring(0,secondDotPosition));
}否则{
System.out.printf(“错误:在'%s'%n',s'中没有第二个点);
}
}
私有静态int findSecondTopPosition(字符串s){
int结果=-1;
int dotsToFind=2;
char[]ca=s.toCharArray();
对于(int i=0;i
对于初学者来说,split的问题是,接受regexp,这就是为什么它在Joop-Eggen的answe中被转义为str.split(\\”)
是的,这可以在一行中实现,正如用户3458271在回答中在与xyz相同的注释中所写,只是错误检查将更加困难(例如,如果没有2个点…。您不能在所有出现的点上拆分字符串,然后只取前两个元素并将它们连接起来吗?String String=“1.0.0.190”;String[]parts=String.split(“.”);零件=零件[0]+“+”零件[1];1) 找到第二个点的位置;2) 在这个位置吐口水。或者在所有点上分开,然后取你想要的任何元素。这可能有助于尚未测试的元素:str.substring(0,str.indexOf('.',1+str.indexOf('.'))为什么这么复杂?
“1.2.3.4.5”
的第一个代码返回{“1.2”,“3.4”}
,同样从维护的角度来看,我不会将替换解决方案称为简单的,因为它是一行程序…@Betlista,因为OP没有为非常基本的东西提供代码,我想展示几把瑞士刀:split
和replaceAll
。我的工作打断了我,所以我只写了“简单”。顺便说一句,xyz有一个很好的答案,尽管只有indexOf
作为“hammer”会使许多解析问题变得难看。
public static void main(String[] args) {
String input = "2.111.1.190";
String[] out = input.split("\\.");
String output1 = out[0]+"."+out[1];
System.out.println(output1);
String output2 = "";
for(int x=2; x < out.length; x++)
output2 += out[x] +".";
System.out.println(output2);
}
public class DotSubstring {
public static void main(String[] args) {
String s = "1.2.3.4";
int secondDotPosition = findSecondDotPosition(s);
if (secondDotPosition > 0) {
System.out.println(s.substring(0, secondDotPosition));
} else {
System.out.printf("ERROR: there is not a 2nd dot in '%s'%n", s);
}
}
private static int findSecondDotPosition(String s) {
int result = -1;
int dotsToFind = 2;
char[] ca = s.toCharArray();
for (int i = 0; i < ca.length; ++i) {
if (ca[i] == '.') --dotsToFind;
if (dotsToFind == 0) return i;
}
return result;
}
}