java 如何使字符串索引从 1 开始

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11753642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:14:55  来源:igfitidea点击:

How to make String indexes start at 1

java

提问by MGPJ

I have a String s = 'muniganesh'and if I print the value of s.subString(1, 2), the output is 'u', because in Java strings, the index starts at 0. But I need to change my string to start with index position 1. How is it possible?

我有一个String s = 'muniganesh',如果我打印 的值s.subString(1, 2),输出是'u',因为在 Java 字符串中,索引从 0 开始。但我需要更改我的字符串以从索引位置 1 开始。这怎么可能?

回答by irrelephant

If you really really want to, you can write a method:

如果你真的很想,你可以写一个方法:

public static String substring(String str, int beginIndex, int endIndex) {
    return str.substring(beginIndex - 1, endIndex - 1);
}

But I highly suggest you don't, since you might mix up s.substring()and your own substring()and get off by 1 errors. Just get accustomed to the way Java handles Strings, and use s.substring(). Many other popular languages start string indexes at 0 like Java.

但我强烈建议你不要,因为你可能会s.substring()和你自己的人混淆,并substring()以 1 个错误下车。只要习惯 Java 处理字符串的方式,并使用s.substring(). 许多其他流行语言像 Java 一样从 0 开始字符串索引。

回答by Art Licis

You could write your own utility method to process both #substring(start, end) arguments as zero-based indexes (or one-based indexes if you wish so), but as @irrelephant said it is not suggested, you should get accustomed to how Java handles these special cases: the first argument is zero-based, while the second one is one-based. String#substringis not the only example, there's also StringBuilder#delete, and there should be more.

您可以编写自己的实用程序方法来将 #substring(start, end) 参数作为从零开始的索引(或从一开始的索引,如果您愿意的话)进行处理,但是正如@irrelephant 所说,不建议这样做,您应该习惯Java 如何处理这些特殊情况:第一个参数是基于零的,而第二个参数是基于一个的String#substring不是唯一的例子,还有StringBuilder#delete,应该还有更多。

The possible motivation could be calculating endposition by simply adding lengthto the startposition without additional increment. E.g.:

可能的动机可能是通过简单地将长度添加到开始位置而不额外增加来计算结束位置。例如:

    String source = "In Java world, end position index may be one-based";
    int indexOfP = source.indexOf('p');
    String result = source.substring(indexOfP, indexOfP + 8);
    System.out.println(result); // prints 'position'

It's not the best example, and the true motivation may differ, but it's how I remember about this peculiarity.

这不是最好的例子,真正的动机可能会有所不同,但这就是我对这种特殊性的记忆。

回答by cl-r

try

尝试

myString = myString.substring(1); // will give "uniganesh"

回答by Next Door Engineer

Java arrays or strings (and the arrays of most other languages) index starting with 0. Make the string one longer and simply don't use the first index, or use indexes from 1 to string.length and simply subtract one when you actually index into the string.

Java 数组或字符串(以及大多数其他语言的数组)索引从 0 开始。将字符串加长一个并且不要使用第一个索引,或者使用从 1 到 string.length 的索引并在实际索引时简单地减去一个入字符串。

回答by Mohammod Hossain

Java string index starts from o an d ends up to string length -1.

Java 字符串索引从 o 开始,到 d 结束,直到字符串长度 -1。

so if you use

所以如果你使用

String s = "muniganesh";

字符串 s = "muniganesh";

s = s.substring(1,2);// output u


System.out.println(""+ s.substring(1)); //output String s = "muniganesh";