Java字符串去掉空格笔记

2020-12-04  本文已影响0人  牧区叔叔

本文摘自:https://www.cnblogs.com/gavincoder/p/9049766.html
亲测实现:前后、中间空格删除
效果

在这里插入图片描述

MainActivity、XML 为什么写个demo呢?AS执行main方法报错,百度一堆都没见效,为了实现样式,就写了个demo。

package com.example.removespacesdemo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button mBt1;
    private Button mBt2;
    private Button mBt3;
    private Button mBt4;
    private Button mBt5;
    private String str = "   212   ~  ds ** 你啊~     ";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        Log.i("Spaces", "原始数据---------》:"+str+str.length());
        mBt1 = (Button) findViewById(R.id.bt1);
        mBt2 = (Button) findViewById(R.id.bt2);
        mBt3 = (Button) findViewById(R.id.bt3);
        mBt4 = (Button) findViewById(R.id.bt4);
        mBt5 = (Button) findViewById(R.id.bt5);
        mBt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = str.trim();
                Log.i("Spaces", "trim---------》:"+trim+trim.length());
            }
        });
        mBt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String replace = str.replace(" ", "");
                Log.i("Spaces", "replace---------》:"+replace+replace.length());
            }
        });
        mBt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = str.replaceAll(" ", "");
                Log.i("Spaces", "replaceAll样式1---------》:"+s+s.length());
            }
        });
        mBt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = str.replaceAll(" +", "");
                Log.i("Spaces", "replaceAll样式2---------》:"+s+s.length());
            }
        });
        mBt5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = str.replaceAll("\\s*", "");
                String s2 = str.replaceAll("\\d", "*");
//                Log.i("Spaces", "replaceAll样式3---------》:"+s+s.length());
                Log.i("Spaces", "replaceAll样式3---------》:"+s2+s2.length());
            }
        });
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/bt1"
        android:text="trim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/bt2"
        android:text="replace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/bt3"
        android:text="replaceAll样式1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/bt4"
        android:text="RepalceAll样式2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/bt5"
        android:text="RepalceAll样式3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
上一篇下一篇

猜你喜欢

热点阅读