Java字符串去掉空客笔记
程序员文章站
2022-03-10 15:23:56
本文摘自: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;impor...
本文摘自: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>
本文地址:https://blog.csdn.net/eerdunsang/article/details/110659643
上一篇: Android编译mk文件引入三方jar、aar、so
下一篇: gradle定义java常量