Java 编写一个程序,允许用户输入一个字符串,然后打印出以逗号分隔的字符串的字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2755012/
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
Write a program that allows the user to enter a string and then prints the letters of the String separated by comma
提问by WM.
The output is always a String, for example H,E,L,L,O,
. How could I limit the commas? I want the commas only between letters, for example H,E,L,L,O
.
输出始终是一个字符串,例如H,E,L,L,O,
。我怎么能限制逗号?我只想要字母之间的逗号,例如H,E,L,L,O
.
import java.util.Scanner;
import java.lang.String;
public class forLoop
{
public static void main(String[] args)
{
Scanner Scan = new Scanner(System.in);
System.out.print("Enter a string: ");
String Str1 = Scan.next();
String newString="";
String Str2 ="";
for (int i=0; i < Str1.length(); i++)
{
newString = Str1.charAt(i) + ",";
Str2 = Str2 + newString;
}
System.out.print(Str2);
}
}
采纳答案by mihsathe
The easiest way I see is :
我看到的最简单的方法是:
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
System.out.print("Enter a string: ");
String Str1 = Scan.nextLine();
String newString="";
String Str2 ="";
for (int i=0; i < Str1.length()-1; i++)
{
newString = Str1.charAt(i) + ",";
Str2 = Str2 + newString;
}
Str2 = Str2 + Str1.charAt(Str1.length()-1);
System.out.println(Str2);
}
The output it will give is :
它会给出的输出是:
run:
Enter a string: Hello world
H,e,l,l,o, ,w,o,r,l,d
BUILD SUCCESSFUL (total time: 5 seconds)
Though I will highly recommend learning regular expression as suggested by @Roman. Till then this will do the trick. :)
尽管我强烈建议您按照@Roman 的建议学习正则表达式。到那时,这将奏效。:)
回答by Woot4Moo
Since this is homework I'll help you out a little without giving the answer:
由于这是家庭作业,我会在不给出答案的情况下帮助您:
If you want the output to only be inbetween letters IE: A,B,C instead of A,B,C, which is what I imagine you are asking about. Then you need to look at your for loop and check the boundary conditions.
如果您希望输出仅在字母 IE 之间:A、B、C 而不是 A、B、C,这就是我想您要问的。然后您需要查看您的 for 循环并检查边界条件。
回答by BalusC
Just don't append the comma when the lastitem of the loop is to be appended. You have the item index by i
and the string length by Str2.length()
. Just do the primary school math with a lesser-than or a greater-than operator in an if
statement.
当要附加循环的最后一项时,不要附加逗号。您有项目索引 byi
和字符串长度 by Str2.length()
。只需在if
语句中使用小于或大于运算符进行小学数学计算即可。
回答by Roman
Try regular expressions:
试试正则表达式:
String input = scanner.next();
String output = input.replaceAll(".", "output = output.substring (0, ouput.length() - 2);
,");
With spaces it would be a bit easier since you don't need to abandon last 'odd' comma:
使用空格会更容易一些,因为您不需要放弃最后一个“奇数”逗号:
System.out.println(Arrays.toString("HELLO".toCharArray()).replaceAll("[\[ \]]", ""));
回答by aioobe
When you've figured out the loop-solution, you could try the following ;)
找出循环解决方案后,您可以尝试以下操作;)
String s = "HELLO";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (i == 0) { // first
sb.append("(" + ch + ")");
} else if (i == s.length() - 1) { // last
sb.append("<" + ch + ">");
} else { // everything in between
sb.append(Character.toLowerCase(ch));
}
}
System.out.println(sb.toString());
// prints "(H)ell<O>"
回答by polygenelubricants
The following snippet should be instructive. It shows:
以下代码段应该具有指导意义。表明:
- How to use
StringBuilder
for building strings - How to process each
char
in aString
using an explicit index- How to detect if it's the first/last iteration for special processing
- 如何使用
StringBuilder
构建字符串 - 如何处理每
char
一个String
使用一个明确的指标- 如何检测它是否是特殊处理的第一次/最后一次迭代
##代码##