Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android TextView में lastIndexOf() का उपयोग कैसे करें?

Android TextView में lastIndexOf() का उपयोग

यह उदाहरण दर्शाता है कि Android TextView में lastIndexOf() मेथड का उपयोग करके किसी सबस्ट्रिंग का अंतिम इंडेक्स कैसे ढूंढा जाता है।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं।

चरण 2: लेआउट XML में कोड जोड़ें

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"
    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 लिया है। जब उपयोगकर्ता बटन पर क्लिक करता है, तो यह डेटा लेता है और "sai" का अंतिम इंडेक्स लौटाता है।

चरण 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) {
                String inputText = name.getText().toString().trim();
                
                if (!inputText.isEmpty()) {
                    // "sai" का अंतिम इंडेक्स ढूंढें
                    int lastIndex = inputText.lastIndexOf("sai");
                    text.setText("\"sai\" का अंतिम इंडेक्स: " + lastIndex);
                } else {
                    name.setError("कृपया नाम दर्ज करें");
                }
            }
        });
    }
}

एप्लिकेशन चलाएं

अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और परिणाम देखें।

Android TextView में lastIndexOf() का उपयोग कैसे करें?

परिणाम: जब आप "Krishna sai sai" दर्ज करते हैं, तो यह "sai" का अंतिम इंडेक्स 12 लौटाता है।

lastIndexOf() के वेरिएंट

String text = "hello world hello";

// 1. सबस्ट्रिंग का अंतिम इंडेक्स
int index1 = text.lastIndexOf("hello"); // 12

// 2. कैरेक्टर का अंतिम इंडेक्स
int index2 = text.lastIndexOf('o'); // 17

// 3. निर्दिष्ट इंडेक्स से पीछे की ओर खोजें
int index3 = text.lastIndexOf("hello", 10); // 0 (इंडेक्स 10 से पहले खोजेगा)

// 4. कैरेक्टर के लिए निर्दिष्ट इंडेक्स से खोजें
int index4 = text.lastIndexOf('l', 5); // 3

मुख्य बिंदु

  • lastIndexOf(String str) - सबस्ट्रिंग के अंतिम मिलान का इंडेक्स लौटाता है
  • lastIndexOf(int ch) - कैरेक्टर के अंतिम मिलान का इंडेक्स लौटाता है
  • lastIndexOf(String str, int fromIndex) - fromIndex से पीछे की ओर खोजता है
  • नहीं मिलने पर -1 लौटाता है
  • केस-सेंसिटिव होता है
  • indexOf() के विपरीत, यह अंत से शुरू होकर खोजता है
  1. Android में StringBuffer का उपयोग कैसे करें? Android में StringBuffer का उपयोग कैसे करें?

    Android में StringBuffer क्लास का उपयोग उदाहरण में जाने से पहले, हमें पता होना चाहिए कि StringBuffer क्या है। StringBuffer क्लास का उपयोग म्यूटेबल स्ट्रिंग बनाने के लिए किया जाता है और यह थ्रेड-सेफ है। यह उदाहरण दर्शाता है कि Android में StringBuffer का उपयोग कैसे करें। चरण 1: नया प्रोजेक्ट बनाएं A

  1. Android में CopyOnWriteArrayList में lastIndexOf() का उपयोग कैसे करें? Android में CopyOnWriteArrayList में lastIndexOf() का उपयोग कैसे करें?

    CopyOnWriteArrayList क्या है? CopyOnWriteArrayList ArrayList का एक थ्रेड-सेफ संस्करण है जो संशोधन ऑपरेशन के लिए अंतर्निहित सरणी की नई कॉपी बनाता है। यह डुप्लिकेट तत्वों की अनुमति देता है। उदाहरण: lastIndexOf() मेथड का उपयोग यह उदाहरण दर्शाता है कि Android में CopyOnWriteArrayList के lastIndexOf()

  1. Android में ButterKnife लाइब्रेरी का उपयोग कैसे करें? Android में ButterKnife लाइब्रेरी का उपयोग कैसे करें?

    Android में ButterKnife से व्यू बाइंडिंग सरल बनाना ButterKnife एक व्यू बाइंडिंग लाइब्रेरी है जो findViewById() कॉल्स को एनोटेशन से बदल देती है। नोट: ButterKnife अब डिप्रिकेटेड है; आधुनिक ऐप्स के लिए View Binding (बिल्ट-इन) या Data Binding उपयोग करें। ⚠ डिप्रिकेटेड: Jake Wharton ने ButterKnife को म