एंड्रॉइड में 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: टेस्ट करें
- ऐप चलाएं
- "tutorialspoint" या कोई 10+ कैरेक्टर का टेक्स्ट दर्ज करें
- "कैरेक्टर्स निकालें" बटन दबाएं
- परिणाम 9वें इंडेक्स से अंत तक का टेक्स्ट दिखाएगा
