从字符串删除空格 [英] Removing spaces from string

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

问题描述

我试图从用户输入得到的字符串中删除所有的空间,但由于某种原因,它不是为我工作。这是我的code。

 公共无效的onClick(查看SRC){
    开关(src.getId()){
        案例R.id.buttonGo:
            字符串输入= EditTextinput.getText()的toString()。
            输入与string.replace =(,);
            URL = UR +输入+ 1;
            意图myIntent =新的意图(start.this,Main.class);
            myIntent.putExtra(URL,网址);
            start.this.startActivity(myIntent);
            打破;
        }
}
 

解决方案

 字符串输入= EditTextinput.getText()的toString()。
输入= input.replace(,);
 

有时候,你想删除只在字符串(不是那些在中间)的开头或结尾的空间。如果是这样的话,你可以使用修剪

 输入= input.trim();
 

I'm trying to remove all the spaces from a string derived from user input, but for some reason it isn't working for me. Here is my code.

public void onClick(View src) {
    switch (src.getId()) {
        case R.id.buttonGo:
            String input = EditTextinput.getText().toString();
            input = String.replace(" ", "");
            url = ur + input + l;
            Intent myIntent = new Intent(start.this, Main.class);
            myIntent.putExtra("URL", url);
            start.this.startActivity(myIntent);
            break;
        }
}

解决方案

String  input = EditTextinput.getText().toString();
input = input.replace(" ", "");

Sometimes you would want to remove only the spaces at the beginning or end of the String (not the ones in the middle). If that's the case you can use trim:

input = input.trim();

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

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