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

एंड्रॉइड TextView में getChars() का उपयोग कैसे करें?

एंड्रॉइड में getChars() मेथड से स्ट्रिंग के कैरेक्टर्स निकालें

यह ट्यूटोरियल दर्शाता है कि getChars() मेथड का उपयोग करके किसी स्ट्रिंग से कैरेक्टर्स की एक रेंज कैसे निकाली जाए और उसे कैरेक्टर ऐरे में कॉपी किया जाए।

चरण 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/inputText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="टेक्स्ट दर्ज करें (न्यूनतम 10 कैरेक्टर)"
        android:inputType="text"
        android:layout_marginBottom="16dp"/>

    <Button
        android:id="@+id/extractButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="कैरेक्टर्स निकालें (9वें से अंत तक)"
        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="#4CAF50"
        android:padding="16dp"
        android:background="#E8F5E9"/>

</LinearLayout>

चरण 3: मेन एक्टिविटी (src/MainActivity.java)

package com.example.getcharsdemo;

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 inputText;
    private Button extractButton;
    private TextView resultText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        inputText = findViewById(R.id.inputText);
        extractButton = findViewById(R.id.extractButton);
        resultText = findViewById(R.id.resultText);

        extractButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = inputText.getText().toString();

                if (input.isEmpty()) {
                    inputText.setError("कृपया टेक्स्ट दर्ज करें");
                    inputText.requestFocus();
                    return;
                }

                if (input.length() < 10) {
                    Toast.makeText(MainActivity.this, 
                        "कम से कम 10 कैरेक्टर दर्ज करें", Toast.LENGTH_SHORT).show();
                    return;
                }

                // getChars() का उपयोग: इंडेक्स 9 से अंत तक कैरेक्टर्स कॉपी करें
                char[] destination = new char[input.length() - 9];
                input.getChars(9, input.length(), destination, 0);

                String extracted = new String(destination);
                resultText.setText("निकाला गया टेक्स्ट: " + extracted);
            }
        });
    }
}

getChars() मेथड सिंटैक्स

public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
पैरामीटरविवरण
srcBeginकॉपी शुरू करने का सोर्स इंडेक्स (इन्क्लूसिव)
srcEndकॉपी समाप्त करने का सोर्स इंडेक्स (एक्सक्लूसिव)
dstडेस्टिनेशन कैरेक्टर ऐरे
dstBeginडेस्टिनेशन ऐरे में शुरू करने का इंडेक्स

उदाहरण

  • इनपुट: "tutorialspoint" (लंबाई 14)
  • getChars(9, 14, dst, 0) इंडेक्स 9 से 13 तक कॉपी करता है
  • परिणाम: "point" (इंडेक्स 9=p, 10=o, 11=i, 12=n, 13=t)

अन्य उपयोगी स्ट्रिंग मेथड्स

// सबस्ट्रिंग निकालने के वैकल्पिक तरीके
String substr1 = input.substring(9);           // इंडेक्स 9 से अंत तक
String substr2 = input.substring(9, 14);       // इंडेक्स 9 से 13 तक
char[] chars = input.toCharArray();            // पूरा स्ट्रिंग कैरेक्टर ऐरे में

चरण 4: टेस्ट करें

  1. ऐप चलाएं
  2. "tutorialspoint" या कोई 10+ कैरेक्टर का टेक्स्ट दर्ज करें
  3. "कैरेक्टर्स निकालें" बटन दबाएं
  4. परिणाम 9वें इंडेक्स से अंत तक का टेक्स्ट दिखाएगा

एंड्रॉइड TextView में getChars() का उपयोग कैसे करें?

  1. Android में स्ट्रिंग तुलना का उपयोग कैसे करें? Android में स्ट्रिंग तुलना का उपयोग कैसे करें?

    Android में स्ट्रिंग तुलना तकनीकें यह उदाहरण दर्शाता है कि Android में स्ट्रिंग तुलना का उपयोग कैसे करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें। चरण 2: लेआउट XML जोड़ें (res/layout/activi

  1. Android में StringBuffer का उपयोग कैसे करें? Android में StringBuffer का उपयोग कैसे करें?

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

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

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