【问题标题】:How to return the 3 middle characters of an odd string using the substring method?如何使用 substring 方法返回奇数字符串的中间 3 个字符?
【发布时间】:2019-01-27 02:37:15
【问题描述】:

我正在尝试使用 substring 方法返回单词的中间 3 个字符,但是如果单词可以是任意大小(仅 ODD),我如何返回单词的中间 3 个字母?

我的代码如下所示。

import java.util.Scanner;
public class Main {
   public static void main(String[] args) {
     Scanner scnr = new Scanner(System.in);
     String inputWord;

     inputWord = scnr.next();

     System.out.println("Enter word: " + inputWord + " Midfix: " + inputWord.substring(2,5));

  }
}

我在 substring 方法中有 2 和 5 的原因是因为我已经用“puzzled”这个词尝试过它,它返回了中间三个字母,就像它应该做的那样。但是如果我尝试,例如“xxxtoyxxx”,它会打印出“xto”而不是“toy”。

附:请不要抨击我,我是编码新手 :)

【问题讨论】:

  • 当我运行您的代码时,“example”按预期返回了“amp”。请澄清您的问题。
  • 我的错,这行得通。我的意思是说如果我输入“xxxtoyxxx”我会得到一个错误。我只是想输入任何奇数长度的单词并得到中间的 3 个字母。因此,即使我输入“car”,它也会返回“car”。

标签: java string substring java.util.scanner


【解决方案1】:

考虑以下代码:

String str = originalString.substring(startingPoint, startingPoint + length)

要确定startingPoint,我们需要找到String 的中间,然后返回我们要检索的length 的一半字符数(在您的情况下为3):

int startingPoint = (str.length() / 2) - (length / 2);

您甚至可以为此构建一个辅助方法:

private String getMiddleString(String str, int length) {
    if (str.length() <= length) {
        return str;
    }

    final int startingPoint = (str.length() / 2) - (length / 2);
    return "[" + str.substring(startingPoint, startingPoint + length) + "]";
}

完整示例:

class Sample {
    public static void main(String[] args) {
        String text = "car";

        System.out.println(getMiddleString(text, 3));
    }

    private static String getMiddleString(String str, int length) {

        // Just return the entire string if the length is greater than or equal to the size of the String
        if (str.length() <= length) {
            return str;
        }

        // Determine the starting point of the text. We need first find the midpoint of the String and then go back
        // x spaces (which is half of the length we want to get.
        final int startingPoint = (str.length() / 2) - (length / 2);
        return "[" + str.substring(startingPoint, startingPoint + length) + "]";

    }
}

在这里,我将输出放在[] 括号中,以反映可能存在的任何空格。上面例子的输出是:[ppl]

使用这种动态方法,您可以在任意长度的String 上运行相同的方法。例如,如果我们的 text 字符串是“这是一个更长的字符串...”,我们的输出将是:[ lo]


注意事项:

  • 如果输入 text 有偶数个字符,但 length 是奇数怎么办? 您需要确定是要向上/向下舍入 length 还是稍微返回一个偏离中心的字符集。

【讨论】:

    【解决方案2】:

    我认为你可以做的是计算字符串长度然后除以 2。这会给你中间的字符串,然后你可以在开头减去 1 并在结尾加上 2。如果要获取奇数字符串的前两个,则在开始索引中减去 2,在结尾处加 1。

    String word_length = inputWord.length()/2;
    System.out.println("Enter word: " + inputWord + " Midfix: " + inputWord.substring((word_length-1, word_length+2));
    

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      这将获取字符串的中间,并返回中间的字符,以及中间索引的+- 1。

      public static String getMiddleThree(String str) {
          int position, length;
          if (str.length() % 2 == 0) {
              position = str.length() / 2 - 1;
              length = 2;
          } else {
              position = str.length() / 2;
              length = 1;
          }
          int start = position >= 1 ? position - 1 : position;
          return str.substring(start, position + 1);
      }
      

      你要做的工作是确保结束位置不大于字符串的长度,否则,选择该位置作为最终索引

      【讨论】:

        猜你喜欢
        • 2017-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-29
        • 2015-07-07
        相关资源
        最近更新 更多