String.endsWith() ব্যবহার করে স্ট্রিং নির্দিষ্ট সাফিক্স (Suffix) দিয়ে শেষ হয় কিনা চেক করা
এই টিউটোরিয়ালে দেখানো হয়েছে কিভাবে EditText-এ ইনপুট করা স্ট্রিংটি নির্দিষ্ট একটি সাবস্ট্রিং (এখানে "point") দিয়ে শেষ হয়েছে কিনা তা endsWith() মেথড দিয়ে চেক করা যায়। রেজাল্ট বুলিয়ান (true/false) হিসেবে TextView-এ দেখানো হয়েছে।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
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="Enter name"
android:layout_height="wrap_content" />
<Button
android:id="@+id/click"
android:text="Click"
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) {
if (!name.getText().toString().isEmpty()) {
if (name.getText().toString().length() >= 0) {
// endsWith() মেথড ব্যবহার
boolean result = name.getText().toString().endsWith("point");
text.setText(String.valueOf(result));
}
} else {
name.setError("Plz enter name");
}
}
});
}
}
অ্যাপ্লিকেশনটি রান করুন
ডিভাইস সংযুক্ত করে Run করুন।
- ইনপুট:
tutorialspoint→ আউটপুট: true (কারণ "point" দিয়ে শেষ) - ইনপুট:
tutorial→ আউটপুট: false


endsWith() মেথডের বৈশিষ্ট্য
- কেস-সেনসিটিভ (Case-sensitive):
"POINT"দিয়ে চেক করলে"tutorialspoint"এর জন্য false দেবে। - খালি স্ট্রিং (
"") যেকোনো স্ট্রিংয়ের সাফিক্স, তাইstr.endsWith("")সবসময়trueরিটার্ন করে। - নাল পাস করলে
NullPointerExceptionফেলে।
বিপরীত মেথড: startsWith()
স্ট্রিং নির্দিষ্ট প্রিফিক্স দিয়ে শুরু হয় কিনা চেক করতে startsWith("prefix") ব্যবহার করুন।