如何在Java中打印字符串的中间三个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39692076/
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-08-11 21:21:32  来源:igfitidea点击:

How to print the middle three characters of a String in Java?

javastringsubstring

提问by Learning_Java

I am new to programming and working on a Java String assignment. The question says:

我是编程和处理 Java 字符串分配的新手。问题说:

  1. Declare a variable of type String named middle3 (put your declaration with the other declarations near the top of the program) and use an assignment statement and thesubstring method to assign middle3 the substring consisting of the middle three characters of phrase (the character at the middle index together with the character to the left of that and the one to the right). Add a println statement to print out the result. Save, compile, and run to test what you have done so far.
  1. 声明一个名为 middle3 的 String 类型变量(将你的声明和其他声明放在程序顶部附近),并使用赋值语句和 substring 方法为 middle3 分配由短语中间三个字符(中间的字符)组成的子串索引以及左边的字符和右边的字符)。添加一个 println 语句来打印结果。保存、编译并运行以测试您到目前为止所做的工作。

Here is what I have done:

这是我所做的:

String phrase = new String ("This is a String test.");
String middle3 = new String ("tri"); //I want to print the middle 3 characters in "phrase" which I think is "tri".

middle3 = phrase.substring (9, 11); // this only prints out 1 letter instead of 3


System.out.println ("middle3: " + middle3);

Here is my output:

这是我的输出:

Original phrase: This is a String test.
Length of the phrase: 18 characters
middle3:  S

I also think that there are 18 characters in the string "phrase", but if there isn't, please let me know. Thank you in advance for all your help!

我也认为字符串“短语”中有 18 个字符,但如果没有,请告诉我。在此先感谢您的帮助!

采纳答案by Mshnik

First off, there are 22 characters in phrase.

首先,有 22 个字符phrase

"This is a String test."
 ^^^^^^^^^^^^^^^^^^^^^^ --> 22 

Note that spaces () count towards the character count. Additionally, Stringhas a method length()which will give you this number.

请注意,空格 ( ) 计入字符数。此外,String还有一种方法length()可以为您提供此数字。

String phrase = "This is a String test.";
int phraseLength = phrase.length(); //22

From there, we can get the middle three characters by working off of the value phraseLength/2. The middle three characters would start one before the middle position, and stop one afterwards. However, because the string(int, int) methodtakes the end index to be exclusive, we should increase it by one.

从那里,我们可以通过处理 value 来获得中间的三个字符phraseLength/2。中间的三个字符会在中间位置之前开始一个,然后停止一个。但是,因为string(int, int) 方法将结束索引设为exclusive,我们应该将其加一。

String phrase = "This is a String test.";
int phraseLength = phrase.length(); //22
String middle3 = phrase.substring(phraseLength/2 - 1, phraseLength/2 + 2); // will have the middle 3 chars.

If the length of phraseis odd, this will return it middle 3 chars. If the length of phraseis even (as it is here), this will return the left-middle 3 chars. (For example, in 1,2,3,4, I'm using left-middle to represent 2and right-middle to represent 3).

如果的长度phrase是奇数,这将返回中间的 3 个字符。如果的长度phrase是偶数(就像这里一样),这将返回左中 3 个字符。(例如,在 中1,2,3,4,我使用 left-middle 来表示2和 right-middle 来表示3)。

Another note, it's both unnecessary and bad practice to write new String("asdf"). Simply use the string literal instead.

另请注意,编写new String("asdf"). 只需使用字符串文字即可。

String phrase = new String ("This is a String test."); //Bad
String phrase = "This is a String test."; //Good

回答by Chris Gong

Think about how to retrieve the middle 3 characters without hardcoding the bounds of the substring()method. You can use the length()method in this regard. For example, the middle character of a string of odd length will always be at index str.length()/2, while the two middle characters of a string of even length will always be at index str.length()/2and (str.length()/2) - 1. So the definition of the three middle characters will be up to you. But for our sakes, we'll just make the 3 middle characters at indexes (str.length()/2)-1, str.length()/2, and (str.length()/2)+1. With this information you can modify this line of code you had before,

考虑如何在不硬编码substring()方法边界的情况下检索中间 3 个字符。您可以length()在这方面使用该方法。例如,奇数长度字符串的中间字符将始终位于 index str.length()/2,而偶数长度字符串的两个中间字符将始终位于 indexstr.length()/2(str.length()/2) - 1。因此,中间三个字符的定义取决于您。但是对于我们的缘故,我们只是在做索引3级中间人物(str.length()/2)-1str.length()/2(str.length()/2)+1。有了这些信息,你就可以修改你之前拥有的这行代码,

middle3 = phrase.substring (9, 11);

to

middle3 = phrase.substring (phrase.length()/2 - 1, phrase.length()/2 + 2);

As to why the original line of code you had before was returning only one letter, it has to do with the parameters of the substringmethod. The first parameter is inclusive, but the second parameter isn't. Therefore, you were only retrieving characters from 9 to 10.

至于为什么你之前的原始代码行只返回一个字母,这与substring方法的参数有关。第一个参数是包含的,但第二个参数不是。因此,您只检索了 9 到 10 个字符。

This is a String test.
         ^^^
        9,10,11 (11 is not included)

The three characters I pointed to are at indices 9, 10, and 11 respectively. You only retrieved the chars ' ' and 'S', which together is just " S". That explains the one-letter output before.

我指向的三个字符分别位于索引 9、10 和 11。您只检索了字符 ' ' 和 'S',它们合起来就是“S”。这解释了之前的单字母输出。

回答by Rubén Soler

you have an error in the begind index and the end index in the string, this is the correct code:

您在字符串中的开始索引和结束索引中有错误,这是正确的代码:

 String phrase = new String("This is a String test.");
 String middle3 = phrase.substring(11, 14); 
 System.out.println("middle3: " + middle3);