从字符串中删除空格

Removing whitespace from strings

我正在处理一个解析任务,我应该接受输入并将其转换为两个单独的字符串并输出这些字符串。如果没有逗号,我会给出错误信息。如果选择是“q”,则结束循环。这是我的代码:

import java.util.Scanner;
import java.lang.*;

public class ParseStrings {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

      /* Type your code here. */
      String name;
      String first;
      String last;
      int commaIndex;
      int size;
      boolean quit = false;
      
      while (!quit)
      {
         System.out.println("Enter input string:");
         name = scnr.nextLine();
         commaIndex = name.indexOf(',');
         size = name.length();
         
         // get q case
         if (name.equals("q"))
         {
            quit = true;
         }
         
         // if no comma, give error message
         else if (commaIndex == -1)
         {
            System.out.println("Error: No comma in string.");
            System.out.println();
         }
         
         else
         {
            first = name.substring(0, commaIndex);
            last = name.substring(commaIndex + 1, size);
            
            // re do, but without spaces 
            
            first = first.replaceAll(" ", "");
            last = last.replaceAll(" ", "");
            commaIndex = name.indexOf(',');
            first = name.substring(0, commaIndex);
            last = name.substring(commaIndex + 1, size);
            
            System.out.println("First word: " + first);
            System.out.println("Second word: " + last);
            System.out.println();
         }
      }
   }
}

输入是这样的:

Jill, Allen
Golden , Monkey
Washington,DC
q

我没有得到分数,因为 space 在单词“Golden”之后和“Monkey”之前

使用String.split(String)它需要一个正则表达式。拆分可选的空格和逗号。喜欢,

String[] tokens = name.trim().split("\s*,\s*");
String first = tokens[0];
String last = tokens[tokens.length - 1];

试试这个

  /* Type your code here. */
  String name;
  String first;
  String last;
  
  while (true)
  {
     System.out.println("Enter input string:");
     name = scnr.nextLine();

     // get q case
     if (name.equals("q"))
     {
        //You can avoid to use quit flag, you can break the loop
       break;
     }
     else
     {

        //Separate the input with split by the comma character
        string [] arr = name.split(",");

         // if no comma, give error message in other words if you array is empty
        if(arr.length==0){
        System.out.println("Error: No comma in string.");
        System.out.println();
        }

        //check if you have more than 2 words
        if(arr.length>1){
            //the trim function will remove all white spaces at the start and end of the string
            first = arr[0].trim();
            last = arr[1].trim();
        System.out.println("First word: " + first);
        System.out.println("Second word: " + last);
        System.out.println();
        }else{
            first =arr[0].trim();
            System.out.println("First word: " + first);
            System.out.println();
        }

     }
  }