如何检查2个charAt(i) [英] How to check 2 charAt(i)
问题描述
我有这行:
812.12 135.14 646.17 1
812.12 135.14 646.18 1
812.12 135.14 646.19 10
812.12 135.14 646.20 10
812.12 135.14 646.21 100
812.12 135.14 646.22 100
我只想删除最后一组,所以我做了如下代码:
I want to delete only the last group so I did code like this:
if(lines[i].charAt(lines[i].length())-1>= '0'&&lines[i].charAt(lines[i].length()-1)<= '9'){
lines[i] = lines[i].substring(0, lines[i].length()-1);
}
else if(lines[i].charAt(lines[i].length())>='10'+c&&lines[i].charAt(lines[i].length()-1)<='99'){
lines[i] = lines[i].substring(0, lines[i].length()-2);
}
else if(lines[i].charAt(lines[i].length())>='100'&&lines[i].charAt(lines[i].length()-1)<='999){
lines[i] = lines[i].substring(0, lines[i].length()-3);
}
现在它对我有用,我需要帮助
And it's now working for me I need help please
推荐答案
您有字符,并试图将它们比较为数字。
You have characters and trying to compare them as numbers. It doesn't work that way.
A char
在大多数高级语言(Java,.NET语言)中都是这样。 ..etc)编程语言默认采用 UTF-16
格式。由于每个 char
大小为 2字节
,因此可以遵循您需要的任何编码。
A char
in most higher-level (Java, .NET languages...etc) programming languages follows UTF-16
format by default. Since each char
size is 2 bytes
it can follow any encoding you need.
您无法将 char
与 string
进行比较不能尝试将字符串放入像 '10'
这样的char中。那是两个字符,而不是一个,它是一个 string
。
You cannot compare a char
to string
and you cannot attempt to put string in char like '10'
. Those are two characters, not one, it's a string
.
现在,根据所需的输出,您想删除最后一个空格之后的所有内容。为此,可以使用以下代码:
Now based on your desired output, you would like to remove everything after the last space. To do that, you can use the following code:
static void updateLines(String[] lines){
for(int i=0;i<lines.length; i++){
// get index of the last space
int index = lines[i].lastIndexOf(" ");
// remove everything after the last space
lines[i] = lines[i].substring(0, index);
}
}
public static void main(String[] args) {
String[] lines = new String[]{
"812.12 135.14 646.17 1",
"812.12 135.14 646.18 1",
"812.12 135.14 646.19 10",
"812.12 135.14 646.20 10",
"812.12 135.14 646.21 100",
"812.12 135.14 646.22 100",
"812.12 135.14 646.23 1000",
"812.12 135.14 646.24 1000"
};
updateLines(lines);
for(int i=0;i<lines.length;i++)
System.out.println(lines[i]);
}
输出:
812.12 135.14 646.17
812.12 135.14 646.18
812.12 135.14 646.19
812.12 135.14 646.20
812.12 135.14 646.21
812.12 135.14 646.22
812.12 135.14 646.23
812.12 135.14 646.24
这够了还是您需要比较数字?
Is this enough or do you need to compare numbers?
这篇关于如何检查2个charAt(i)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!