给定字符串是 ' _ 家庭甜蜜的家 __ ' 如果用户输入模式为0,那么o / p应为'home sweet home_ _ ' 如果用户输入模式为1,则o / p应为' _home sweet home' 如果用户输入模式为2,那么o / p应该是'home sweet home'。
的代码 的
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String");
String str=sc.nextLine();
System.out.println("Enter the StringMode");
String strMode= sc.nextLine();
switch()
{
}
我想找到给定字符串中的空格总数。
答案 0 :(得分:1)
Do this删除所有空格。然后,从第一个字符串的长度中减去第二个字符串的长度,以确定删除的空格总数。
如果您只想删除前面的空格,请使用“^ \\ s +”。要删除尾随,请使用“\\ s + $”。
答案 1 :(得分:0)
试试这个
StringTokenizer t = new StringTokenizer(str," ");
result = t.nextToken();
Boolean first = str.toCharArray()[0]==' ';
Boolean last = str.toCharArray()[str.length()-1]==' ';
while(t.hasMoreTokens())
{
result += " " + t.nextToken();
}
switch(strMode)
{
case 0 : if(last) result += " ";
break;
case 1 : if(first) result = " " + result;
break;
}
System.out.println(result);
答案 2 :(得分:0)
您可以尝试这样的事情:
/**
* Remove white spaces from string according to mode
*
* @param str string
* @param mode mode 0=leading, 1=trailing, 2=leading+trailing
* @param result - result buffer
* @return number of white spaces removed
*/
public int removeWhiteSpacesByMode(String str, int mode, StringBuilder result) {
int n = 0;
switch(mode) {
case 0:
n = removeLeadingWhiteSpaces(str, result);
break;
case 1:
n = removeTrailingWhiteSpaces(str, result);
break;
case 2:
StringBuilder tmp = new StringBuilder();
n = removeTrailingWhiteSpaces(str, tmp);
n += removeLeadingWhiteSpaces(tmp.toString(), result);
break;
default:
throw new IllegalArgumentException("mode=" + mode);
}
return n;
}
private int removeTrailingWhiteSpaces(String str, StringBuilder result) {
int n = 0;
if(str != null && result != null) {
n = str.length()-1;
while(Character.isWhitespace(str.charAt(n))) {
n--;
}
n++;
for(int j = 0; j < n; j++) {
result.append(str.charAt(j));
}
n = str.length() - n;
}
return n;
}
private int removeLeadingWhiteSpaces(String str, StringBuilder result) {
int n = 0;
if(str != null && result != null) {
while(Character.isWhitespace(str.charAt(n))) {
n++;
}
for(int j = n; j < str.length(); j++) {
result.append(str.charAt(j));
}
}
return n;
}
它使用方法Character#isWhitespace来检查字符是否为空格,并使用StringBuilder来构建结果。返回值是删除的白色步数。
如果你想要一个只计算字符串中空格的方法,你可以循环遍历整个字符串,使用Character#isWhitespace检查每个字符,如果它返回true,则递增一个变量。
最后这里有一些测试:
@Test
public void removeWhiteSpacesByMode() {
String str = " home sweet home ";
StringBuilder result = null;
int numberOfWhiteSpacesRemoved = 0;
numberOfWhiteSpacesRemoved = removeWhiteSpacesByMode(str, 0, null);
Assert.assertEquals(numberOfWhiteSpacesRemoved, 0);
result = new StringBuilder();
numberOfWhiteSpacesRemoved = removeWhiteSpacesByMode(null, 0, result);
Assert.assertEquals(0, result.length());
Assert.assertEquals(numberOfWhiteSpacesRemoved, 0);
try {
result = new StringBuilder();
numberOfWhiteSpacesRemoved = 0;
numberOfWhiteSpacesRemoved = removeWhiteSpacesByMode(null, 4, result);
Assert.fail("mode 4 should not have been accepted");
} catch(IllegalArgumentException e) {
Assert.assertEquals("mode=4", e.getMessage());
Assert.assertEquals(0, result.length());
Assert.assertEquals(numberOfWhiteSpacesRemoved, 0);
}
result = new StringBuilder();
numberOfWhiteSpacesRemoved = removeWhiteSpacesByMode(str, 0, result);
Assert.assertEquals("home sweet home ", result.toString());
Assert.assertEquals(numberOfWhiteSpacesRemoved, 1);
result = new StringBuilder();
numberOfWhiteSpacesRemoved = removeWhiteSpacesByMode(str, 1, result);
Assert.assertEquals(" home sweet home", result.toString());
Assert.assertEquals(numberOfWhiteSpacesRemoved, 2);
result = new StringBuilder();
numberOfWhiteSpacesRemoved = removeWhiteSpacesByMode(str, 2, result);
Assert.assertEquals("home sweet home", result.toString());
Assert.assertEquals(numberOfWhiteSpacesRemoved, 3);
}
答案 3 :(得分:0)
基于正则表达式的解决方案,用于捕获空格,然后根据您需要的模式重建字符串。没有循环,但需要一些知识。
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String");
String str=sc.nextLine();
System.out.println("Enter the StringMode");
int strMode= sc.nextInt();
Pattern pattern = Pattern.compile("^(?<leadingWs>\\s*)(?<text>.+?)(?<trailingWs>\\s*)$");
Matcher matcher = pattern.matcher(str);
matcher.matches(); // should match always
String result = "";
switch(strMode)
{
case 0:
result = matcher.group("text") + matcher.group("trailingWs");
break;
case 1:
result = matcher.group("leadingWs") + matcher.group("text");
break;
case 2:
result = matcher.group("text");
break;
default:
break;
}
System.out.println("Cleared string: \"" + result + "\"");
System.out.println("Leading whitespace characters: " + matcher.group("leadingWs").length());
System.out.println("Trailing whitespace characters: " + matcher.group("trailingWs").length());
}
它使用命名捕获组来提取空白和不情愿的量词,以便在追踪空格字符之前获取所有文本。有关捕获组的信息,请参阅Pattern documentation;有关量化器的工作原理,请参阅this教程。
答案 4 :(得分:0)
一种简单的方法:
private static String truncateSpace(String text, int mode)
{
if(mode==0 || mode==2)
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) != ' ') {
text = text.substring(i, text.length());
break;
}
}
if(mode==1 || mode==2)
for (int i = text.length()-1; i > 0; i--) {
if (text.charAt(i) != ' ') {
text = text.substring(0, i+1);
break;
}
}
return text;
}