एंड्रॉइड में indexOf() मेथड का उपयोग करके सबस्ट्रिंग खोजें
यह ट्यूटोरियल दर्शाता है कि एंड्रॉइड ऐप में indexOf() मेथड का उपयोग करके किसी स्ट्रिंग में विशिष्ट सबस्ट्रिंग की स्थिति कैसे खोजी जाए। हम एक EditText से इनपुट लेंगे और "sai" शब्द का इंडेक्स प्रदर्शित करेंगे।
चरण 1: नया प्रोजेक्ट बनाएं
एंड्रॉइड स्टूडियो में File → New Project → Empty Activity चुनें और प्रोजेक्ट सेटअप पूरा करें।
चरण 2: लेआउट डिज़ाइन करें (res/layout/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"
android:padding="24dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/nameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="नाम दर्ज करें"
android:inputType="text"
android:layout_marginBottom="16dp"/>
<Button
android:id="@+id/findButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="इंडेक्स खोजें"
android:layout_marginBottom="16dp"/>
<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#2196F3"/>
</LinearLayout>
चरण 3: लॉजिक कार्यान्वित करें (src/MainActivity.java)
package com.example.indexofdemo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText nameInput;
private Button findButton;
private TextView resultText;
private static final String SEARCH_TERM = "sai";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameInput = findViewById(R.id.nameInput);
findButton = findViewById(R.id.findButton);
resultText = findViewById(R.id.resultText);
findButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = nameInput.getText().toString().trim();
if (input.isEmpty()) {
nameInput.setError("कृपया नाम दर्ज करें");
nameInput.requestFocus();
return;
}
// indexOf() का उपयोग करके सबस्ट्रिंग खोजें
int index = input.indexOf(SEARCH_TERM);
if (index >= 0) {
resultText.setText("शब्द '" + SEARCH_TERM + "' इंडेक्स " + index + " पर मिला");
} else {
resultText.setText("शब्द '" + SEARCH_TERM + "' नहीं मिला (परिणाम: -1)");
}
}
});
}
}
indexOf() मेथड समझाया गया
| मेथड | विवरण |
|---|---|
indexOf(String str) | निर्दिष्ट सबस्ट्रिंग की पहली घटना का इंडेक्स लौटाता है, नहीं मिलने पर -1 |
indexOf(String str, int fromIndex) | निर्दिष्ट इंडेक्स से खोज शुरू करता है |
lastIndexOf(String str) | अंतिम घटना का इंडेक्स लौटाता है |
उदाहरण आउटपुट
- इनपुट:
"Krishna sai"→ आउटपुट:"शब्द 'sai' इंडेक्स 8 पर मिला" - इनपुट:
"Sai Kumar"→ आउटपुट:"शब्द 'sai' इंडेक्स 0 पर मिला"(केस-सेंसिटिव) - इनपुट:
"Rahul Sharma"→ आउटपुट:"शब्द 'sai' नहीं मिला (परिणाम: -1)"
केस-इनसेंसिटिव खोज के लिए
// लोअरकेस में कनवर्ट करके केस-इनसेंसिटिव खोज int index = input.toLowerCase().indexOf(SEARCH_TERM.toLowerCase());
चरण 4: टेस्ट करें
- ऐप को वास्तविक डिवाइस पर चलाएं
- EditText में कोई नाम दर्ज करें (उदाहरण: "Krishna sai")
- "इंडेक्स खोजें" बटन पर टैप करें
- परिणाम TextView में प्रदर्शित होगा
