Android में TextView से व्हाइटस्पेस हटाना
यह उदाहरण दर्शाता है कि Android में TextView से व्हाइटस्पेस (रिक्त स्थान) कैसे हटाए जाते हैं।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल में UI जोड़ें (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:hint="नाम दर्ज करें"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/save"
android:text="सेव करें"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
उपरोक्त कोड में, हमने नाम के लिए EditText लिया है। जब उपयोगकर्ता बटन पर क्लिक करता है, तो EditText का मान लेकर व्हाइटस्पेस हटाकर TextView में दिखाया जाता है।
चरण 3: MainActivity.java में लॉजिक जोड़ें
package com.example.myapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText name;
TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
textview = findViewById(R.id.textview);
findViewById(R.id.save).setOnClickListener(v -> {
String input = name.getText().toString();
if (!input.isEmpty()) {
// सभी व्हाइटस्पेस हटाएं (स्पेस, टैब, नई लाइन)
String result = input.replaceAll("\\s+", "");
textview.setText(result);
Toast.makeText(this, "व्हाइटस्पेस हटाया गया", Toast.LENGTH_LONG).show();
} else {
name.setError("नाम दर्ज करें");
}
});
}
}
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में Run आइकन पर क्लिक करें और अपना डिवाइस चुनें।

ऊपर दिए गए परिणाम में, हम नाम दर्ज कर रहे हैं लेकिन TextView में कोई व्हाइटस्पेस नहीं है। replaceAll("\\s+", "") रेगुलर एक्सप्रेशन सभी प्रकार के व्हाइटस्पेस कैरेक्टर्स को हटा देता है।