不使用trim方法从字符串中删除空格? [英] Remove white space from string without using trim method?

查看:206
本文介绍了不使用trim方法从字符串中删除空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定字符串为
' _ home sweet home __ '
如果用户输入模式为0则o / p应为'home如果用户输入模式为1,那么o / home应该为' _home sweet home'$ b $,$ home $ <$> $ b
b如果用户输入模式为2,则o / p应为'home sweet home'。

given string is '_home sweet home__' if user enter the mode as 0 then o/p should be 'home sweet home__' if user enter the mode as 1 then o/p should be '_home sweet home' if user enter the mode as 2 then o/p should be 'home sweet home'.

代码

Code

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()
{

}

我想找到给定字符串中的空格总数。

i want to find total number of white spaces in the given string.

推荐答案

试试这个

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);

这篇关于不使用trim方法从字符串中删除空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆