例如,我想检查一下,当我分割我的字符串时,第一部分只包含数字和小数点.
我做了以下事情
String[] s1 = {"0.12.13.14-00000001-00000", "0.12.13.14-00000002-00000"};
String[] parts_s1 = s1.split("-");
System.out.println(parts_s1[0]);
if(parts_s1[0].matches("[0-9]")
Run Code Online (Sandbox Code Playgroud)
但那仅仅是检查数字而不是小数.我怎样才能检查这个小数?例如,我想检查0.12.13.14是否有效,0.12.13.14x之类的东西不起作用.
我试图排序以下字符串
1.0.0.0-00000000-00000
2.1.0.0
2.2.0.0
2.3.0.0-00000000-00000
Run Code Online (Sandbox Code Playgroud)
我目前在一个字符串数组中有这些值.
String[] arrays = {"1.0.0.0-00000000-00000", "2.1.0.0", "2.2.0.0", "2.3.0.0-00000000-00000"};
Run Code Online (Sandbox Code Playgroud)
我试图有一个输出,如果没有" - "那么这些值按排序顺序到达我的数组的末尾.我想要输出如下:
1.0.0.0-00000000-00000
2.3.0.0-00000000-00000
2.1.0.0
2.2.0.0
Run Code Online (Sandbox Code Playgroud)
我已经尝试Arrays.sort(arrays)但是我不确定如何对此进行排序?
import java.util.Arrays;
import java.util.Comparator;
import java.util.Collections;
public class HelloWorld{
public static void main(String []args){
String[] arrays = {"1.0.0.0-00000000-00000", "2.1.0.0", "2.2.0.0", "2.3.0.0-00000000-00000"};
String[] newArray = new String[arrays.length];
class CustomComparator implements Comparator<String>
{
@Override
public int compare(String a, String b)
{
if(a.contains("-") && !b.contains("-"))
return 1;
else if(!a.contains("-") && b.contains("-"))
return -1;
return a.compareTo(b);
}
}
Arrays.sort(arrays, new CustomComparator()); …Run Code Online (Sandbox Code Playgroud) 我已经阅读了多个 stackoverflow 帖子,但似乎没有一个能帮助我理解我的错误在哪里。我试图有两个单选按钮,默认情况下选中长格式而不选中短格式,但如果选中短格式,则不选中长格式。如果还要取消选中短格式,则默认情况下应选中长格式。
<input type="radio" id="shortForm" onChange="toggle(this);"> Short Form
<input type="radio" id="longForm" onChange="toggle(this);"> Long Form
function toggle(chkBox)
{
if (chkBox.checked)
{
var previousCheckId;
if (previousCheckId)
{
document.getElementById(previousCheckId).checked = false;
}
previousCheckId = chkBox.getAttribute('id');
}
}
Run Code Online (Sandbox Code Playgroud)
我试图避免使用 jquery 而只使用纯 javascript 和 html。
到目前为止,我得到了这个,但我的话没有正确对齐?我怎样才能让它们对齐?
<input type="radio" id="shortForm" name="frm"style="position:absolute; top:10px; right:875px"> Short Form </input>
<input type="radio" id="longForm" name="frm" style="position:absolute; top:10px; right:475px"> Long Form </input>
Run Code Online (Sandbox Code Playgroud) 在其他语言中,我知道我可以做到以下几点.但我在Java中理解为什么我不能这样做有困难.我的错误是:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The operator || is undefined for the argument type(s) java.lang.String, java.lang.String
我的代码是:
public class Tester {
public static void main(String[] args)
{
String one = null;
String two = "two";
String three = one || two;
System.out.println(three);
}
}
Run Code Online (Sandbox Code Playgroud)