StringBuffer से स्ट्रिंग रिवर्स करना
यह उदाहरण दर्शाता है कि Android में स्ट्रिंग को रिवर्स कैसे किया जाता है। StringBuffer क्लास का reverse() मेथड इसमें उपयोगी है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल (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>
चरण 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()) {
// StringBuffer का उपयोग करके स्ट्रिंग रिवर्स करें
StringBuffer buffer = new StringBuffer(input);
textview.setText(buffer.reverse());
Toast.makeText(this, "रिवर्स किया गया", Toast.LENGTH_LONG).show();
} else {
name.setError("नाम दर्ज करें");
}
});
}
}
ऐप चलाएं
डिवाइस कनेक्ट करें और ऐप चलाएं।

परिणाम में, हम नाम दर्ज कर रहे हैं लेकिन TextView उसे रिवर्स ऑर्डर में दिखा रहा है। StringBuffer थ्रेड-सेफ है; सिंगल-थ्रेडेड उपयोग के लिए StringBuilder तेज़ है।