एंड्रॉइड में String.charAt() से विशिष्ट इंडेक्स का कैरेक्टर प्राप्त करना
यह ट्यूटोरियल दिखाता है कि EditText इनपुट से विशिष्ट इंडेक्स (जैसे 5वां कैरेक्टर) पर charAt() का उपयोग करके कैरेक्टर कैसे प्राप्त करें।
चरण 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: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/name"
android:layout_width="match_parent"
android:hint="टेक्स्ट दर्ज करें (न्यूनतम 6 अक्षर)"
android:layout_height="wrap_content" />
<Button
android:id="@+id/click"
android:text="5वां कैरेक्टर प्राप्त करें"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:textSize="25sp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:textColor="#333" />
</LinearLayout>
चरण 3: MainActivity कोड (java/MainActivity.java)
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText nameEditText;
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.name);
button = findViewById(R.id.click);
textView = findViewById(R.id.textview);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = nameEditText.getText().toString();
if (!input.isEmpty()) {
// इंडेक्स 5 (6वां कैरेक्टर, 0-आधारित)
int index = 5;
if (input.length() > index) {
char ch = input.charAt(index);
textView.setText(index + "वें इंडेक्स का कैरेक्टर: '" + ch + "'");
} else {
textView.setText("इनपुट बहुत छोटा है (न्यूनतम " + (index + 1) + " अक्षर चाहिए)");
}
} else {
nameEditText.setError("कृपया टेक्स्ट दर्ज करें");
}
}
});
}
}
charAt() मुख्य बिंदु
- 0-आधारित इंडेक्स: पहला कैरेक्टर index 0 पर
- रिटर्न टाइप:
char(प्रिमिटिव) - अमान्य इंडेक्स पर
IndexOutOfBoundsExceptionफेंकता है - हमेशा
length()से जांचें पहले
सुरक्षित उपयोग पैटर्न
// सुरक्षित विधि
public static Character safeCharAt(String str, int index) {
if (str != null && index >= 0 && index < str.length()) {
return str.charAt(index);
}
return null;
}
// उपयोग
Character ch = safeCharAt(input, 5);
if (ch != null) {
textView.setText("कैरेक्टर: " + ch);
} else {
textView.setText("अमान्य इंडेक्स");
}
संबंधित विधियाँ
| विधि | विवरण |
|---|---|
charAt(int index) | इंडेक्स पर सिंगल कैरेक्टर |
codePointAt(int index) | यूनिकोड कोड पॉइंट (इमोजी/सप्लीमेंट्री के लिए) |
getChars(srcBegin, srcEnd, dst, dstBegin) | कैरेक्टर ऐरे में कॉपी करें |
substring(start, end) | सबस्ट्रिंग प्राप्त करें |
इमोजी/यूनिकोड के लिए
String emoji = "👋🌍"; // charAt(0) → केवल पहला सरोगेट (अधूरा) // codePointAt(0) → पूरा कोड पॉइंट (128071) int cp = emoji.codePointAt(0); String fullChar = new String(Character.toChars(cp)); // "👋"
ऐप टेस्ट करें
"HelloWorld" दर्ज करें → इंडेक्स 5 पर 'W' मिलेगा (H=0, e=1, l=2, l=3, o=4, W=5)।