给定的字符串为“ _home sweet home __* ”,如果用户输入模式为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() { }
我想找到给定字符串中空格的总数。
试试这个
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);