EditText इनपुट से अतिरिक्त स्पेस हटाना
String.trim() मेथड का उपयोग करके उपयोगकर्ता द्वारा दर्ज टेक्स्ट से शुरुआत और अंत के व्हाइटस्पेस हटाना।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:hint="नाम दर्ज करें"
android:layout_height="wrap_content" />
<Button
android:id="@+id/click"
android:text="क्लिक करें"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:textSize="25sp"
android:layout_height="wrap_content" />
</LinearLayout>
ऊपर दिए गए कोड में EditText इनपुट, बटन और परिणाम दिखाने के लिए TextView है।
चरण 3: MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText name;
Button button;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
button = findViewById(R.id.click);
text = findViewById(R.id.textview);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!name.getText().toString().isEmpty()) {
if (name.getText().toString().length() >= 0) {
String trim = name.getText().toString().trim();
text.setText(String.valueOf(trim));
}
} else {
name.setError("कृपया नाम दर्ज करें");
}
}
});
}
}
परिणाम
यदि उपयोगकर्ता " krishna " (स्पेस के साथ) दर्ज करता है, तो परिणाम "krishna" (बिना स्पेस) दिखाएगा।

उपरोक्त परिणाम में, स्ट्रिंग " krishna " दर्ज की गई और इसने बिना स्पेस के "krishna" वापस किया।