如何在 Java 中打印出字符串中的特定字母

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

How to print out specific letters within a String in Java

java

提问by ProgrammingBeginner

I have been set a task to print out the 2nd, 3rd and 4th letter of a name within a String variable.

我已经设置了一个任务来打印出字符串变量中名称的第 2、3 和 4 个字母。

So for example the name is John and I want to print out the letters 'o, h and n only'

例如,名字是约翰,我只想打印出字母“o、h 和 n”

I was wondering if there is a specific method which I can use to carry out this task, if not what is the best approach to take?

我想知道是否有一种特定的方法可以用来执行这项任务,如果没有,最好的方法是什么?

采纳答案by robotlos

EDITas @KevinO pointed out, determining whether the name length or 4 is smaller solves issues that would cause exceptions. I updated to incorporate this input.

Depends on how you're trying to print it. You can use a forloop and iterate starting at the 1st index of your String namelike so:

正如@KevinO 指出的那样编辑,确定名称长度或 4 是否更小可以解决会导致异常的问题。我更新以合并此输入。

取决于您尝试打​​印它的方式。您可以使用for循环并从您String name喜欢的第一个索引开始迭代:

String name = "John";
for(int i = 1; i < Math.min(name.length(), 4); i++){
    System.out.print(name.charAt(i));
}

Sample Run:

示例运行:

run:
ohn
BUILD SUCCESSFUL (total time: 0 seconds)

运行:
ohn
BUILD SUCCESSFUL(总时间:0 秒)

You could print out theCharacterone at a time like:

您可以一次打印出Character一个,例如:

System.out.print(name.charAt(1)); //print character at index 1
System.out.print(name.charAt(2)); //print character at index 2
System.out.print(name.charAt(3)); //print character at index 3

This might be unsafe because you're not sure if the name will in be in fact at least 4 Characterslong. Sample run:

这可能不安全,因为您不确定名称实际上是否至少为 4Characters长。示例运行:

run:
ohn
BUILD SUCCESSFUL (total time: 0 seconds)

运行:
ohn
BUILD SUCCESSFUL(总时间:0 秒)

Or perhaps the easiest way which is also safe, you could print it out using String.substring()which takes in a range, like so:

或者也许是最简单的方法也是安全的,您可以使用String.substring()它在一个范围内打印出来,如下所示:

    System.out.println(name.substring(1, Math.min(name.length(), 4)));

This results in:

这导致:

run:
ohn
BUILD SUCCESSFUL (total time: 0 seconds)

运行:
ohn
BUILD SUCCESSFUL(总时间:0 秒)

回答by Sajeev

You can do that by using charAt function on String . The following version of code should do what you are asking for.

您可以通过在 String 上使用 charAt 函数来做到这一点。以下版本的代码应该可以满足您的要求。

public static void main(String[] args) {
    String input = "John";
    if (input != null && input.length() > 1) { 
               System.out.println(input.substring(1, Math.min(input.length(), 4)));     
    }
}

回答by Aaron

Assuming the name always has at least 4 letters in it, this code will work:

假设名称中始终至少包含 4 个字母,则此代码将起作用:

public static void main(String[] args) {
    String name = "JOHN";
    System.out.println(name.substring(1,4));
}