অ্যান্ড্রয়েড TextView-এ getChars() ব্যবহার
এই উদাহরণে দেখানো হয়েছে কিভাবে getChars() মেথড ব্যবহার করে একটি স্ট্রিং থেকে নির্দিষ্ট রেঞ্জের ক্যারেক্টারগুলি ক্যারেক্টার অ্যারেতে কপি করা যায়।
প্রয়োজনীয় ধাপসমূহ
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project নির্বাচন করে প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট ডিজাইন করুন
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>
ধাপ ৩: জাভা কোড লিখুন
src/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();
if (!inputText.isEmpty()) {
// ৯ম ইনডেক্স থেকে শেষ পর্যন্ত ক্যারেক্টার কপি করা
int startIndex = 9;
int endIndex = inputText.length();
if (startIndex < endIndex) {
char[] destination = new char[endIndex - startIndex];
inputText.getChars(startIndex, endIndex, destination, 0);
text.setText("ফলাফল: " + new String(destination));
} else {
text.setText("ইনপুট খুব ছোট (ন্যূনতম ১০ অক্ষর দরকার)");
}
} else {
name.setError("দয়া করে টেক্সট লিখুন");
}
}
});
}
}
getChars() মেথডের সিনট্যাক্স
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
প্যারামিটার ব্যাখ্যা
| প্যারামিটার | ব্যাখ্যা |
|---|---|
| srcBegin | কপি শুরু করার সূচক ইনডেক্স (ইনক্লুসিভ) |
| srcEnd | কপি শেষ করার সূচক ইনডেক্স (এক্সক্লুসিভ) |
| dst | গন্তব্য ক্যারেক্টার অ্যারে |
| dstBegin | গন্তব্য অ্যারেতে কপি শুরু করার ইনডেক্স |
উদাহরণ ফলাফল
ইনপুট: "tutorialspoint" (১৪ অক্ষর)
শুরু: ৯, শেষ: ১৪
আউটপুট: "point"
কারণ ইনডেক্স ৯ থেকে ১৩ পর্যন্ত (point) কপি হয়েছে।
সাবধানতা
- ArrayIndexOutOfBoundsException এড়াতে গন্তব্য অ্যারের আকার যথেষ্ট রাখুন
- StringIndexOutOfBoundsException এড়াতে ইনডেক্স যাচাই করুন
- নাল বা খালি স্ট্রিং-এ কল করলে এক্সেপশন হবে
বিকল্প পদ্ধতি: substring()
সহজ পদ্ধতিতে:
String result = inputText.substring(9); // ৯ম ইনডেক্স থেকে শেষ পর্যন্ত
text.setText("ফলাফল: " + result);
অ্যাপ্লিকেশন চালানো
ডিভাইস সংযুক্ত করে Run করুন। "tutorialspoint" লিখে বাটনে ক্লিক করলে "point" দেখাবে।