1. স্ট্রিং তুলনা

এই সব ভাল এবং ভাল. কিন্তু আপনি দেখতে পাচ্ছেন যে s1এবং s2স্ট্রিংগুলি আসলে একই, যার অর্থ তাদের একই পাঠ্য রয়েছে। স্ট্রিং তুলনা করার সময়, আপনি কীভাবে প্রোগ্রামটিকে Stringবস্তুর ঠিকানার দিকে নয়, তাদের বিষয়বস্তুর দিকে তাকাতে বলবেন?

এটিতে আমাদের সাহায্য করার জন্য, জাভার Stringক্লাসের পদ্ধতি রয়েছে equals। কল করা এই মত দেখায়:

string1.equals(string2)
দুটি স্ট্রিং তুলনা

trueস্ট্রিং একই হলে এই পদ্ধতিটি ফেরত দেয় , এবং falseযদি তারা একই না হয়।

উদাহরণ:

কোড বিঃদ্রঃ
String s1 = "Hello";
String s2 = "HELLO";
String s3 = s1.toUpperCase();

System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s2.equals(s3));
// Hello
// HELLO
// HELLO

false // They are different
false // They are different
true // They are the same, even though the addresses are different

আরো উদাহরণ:

কোড ব্যাখ্যা
"Hello".equals("HELLO")
false
String s = "Hello";
"Hello".equals(s);
true
String s = "Hel";
"Hello".equals(s + "lo");
true
String s = "H";
(s + "ello").equals(s + "ello");
true

4
কাজ
Java Syntax,  লেভেল 4পাঠ 6
লক করা আছে
Minimum of two numbers
All search and sort algorithms are based on comparisons. You'll be able to handle these very soon, if you so desire. In the meantime, we suggest starting with something small: write a program to find the minimum of two numbers. Find it and then display it. And if the numbers are the same, display either of them.
4
কাজ
Java Syntax,  লেভেল 4পাঠ 6
লক করা আছে
Maximum of four numbers
Finding the maximum is an n-ary operation (an operation on n numbers) that returns the largest of several numbers. Never mind. We have no need for such definitions at the secret CodeGym center. We're here to learn how to write code. In this task, you need to use the keyboard to enter four numbers. Then determine the largest of them and display it on the screen.

2. কেস-সংবেদনশীল স্ট্রিং তুলনা

শেষ উদাহরণে, আপনি দেখেছেন যে তুলনা ফলন । প্রকৃতপক্ষে, স্ট্রিংগুলি সমান নয়। কিন্তু..."Hello".equals("HELLO")false

স্পষ্টতই, স্ট্রিংগুলি সমান নয়। এটি বলেছে, তাদের বিষয়বস্তুতে একই অক্ষর রয়েছে এবং শুধুমাত্র অক্ষরের ক্ষেত্রে ভিন্ন। তাদের তুলনা করার এবং চিঠির ক্ষেত্রে উপেক্ষা করার কোন উপায় আছে কি? অর্থাৎ, এত ফলন ?"Hello".equals("HELLO")true

আর এই প্রশ্নের উত্তর হল হ্যাঁ। জাভাতে, Stringটাইপের আরেকটি বিশেষ পদ্ধতি রয়েছে: equalsIgnoreCase. কল করা এই মত দেখায়:

string1.equalsIgnoreCase(string2)

পদ্ধতির নামটি মোটামুটি তুলনা করে অনুবাদ করে কিন্তু কেস উপেক্ষা করে । পদ্ধতির নামের অক্ষরে দুটি উল্লম্ব লাইন রয়েছে: প্রথমটি একটি ছোট হাতের অক্ষর Lএবং দ্বিতীয়টি একটি বড় হাতের অক্ষর i। এটি আপনাকে বিভ্রান্ত করতে দেবেন না।

উদাহরণ:

কোড বিঃদ্রঃ
String s1 = "Hello";
String s2 = "HELLO";
String s3 = s1.toUpperCase();

System.out.println(s1.equalsIgnoreCase(s2));
System.out.println(s1.equalsIgnoreCase(s3));
System.out.println(s2.equalsIgnoreCase(s3));
// Hello
// HELLO
// HELLO

true
true
true

8
কাজ
Java Syntax,  লেভেল 4পাঠ 6
লক করা আছে
Sorting three numbers
Planet Linear Chaos is populated by isomorphs. They are believed to have invented sorting algorithms. Everything in their heads is extremely well-ordered. They only issue planetary visas to people who know at least 7 sorting algorithms. Let's take our first step toward Linear Chaos: Read three numbers from the keyboard, put them in descending order, and then display them on the screen.

3. স্ট্রিং তুলনার উদাহরণ

শুধু একটি সহজ উদাহরণ দেওয়া যাক: ধরুন আপনাকে কীবোর্ড থেকে দুটি লাইন লিখতে হবে এবং সেগুলি একই কিনা তা নির্ধারণ করতে হবে। এই কোডটি দেখতে কেমন হবে:

Scanner console = new Scanner(System.in);
String a = console.nextLine();
String b = console.nextLine();
String result = a.equals(b) ? "Same" : "Different";
System.out.println(result);

4. স্ট্রিং তুলনা একটি আকর্ষণীয় nuance

একটি গুরুত্বপূর্ণ সূক্ষ্মতা রয়েছে যা আপনাকে সচেতন হতে হবে।

যদি জাভা কম্পাইলার আপনার কোডে (বিশেষ করে আপনার কোডে) একাধিক অভিন্ন স্ট্রিং খুঁজে পায়, তাহলে মেমরি সংরক্ষণ করার জন্য এটি তাদের জন্য শুধুমাত্র একটি একক বস্তু তৈরি করবে।

String text = "This is a very important message";
String message = "This is a very important message";

এবং ফলস্বরূপ মেমরিতে কী থাকবে তা এখানে:

স্ট্রিং তুলনা

এবং যদি আপনি text == messageএখানে তুলনা করেন, তাহলে আপনি পাবেন true। তাই এতে অবাক হবেন না।

যদি কোনও কারণে আপনার সত্যিই রেফারেন্সগুলি আলাদা হওয়ার প্রয়োজন হয় তবে আপনি এটি লিখতে পারেন:

String text = "This is a very important message";
String message = new String ("This is a very important message");

অথবা এটা:

String text = "This is a very important message";
String message = new String (text);

এই উভয় ক্ষেত্রেই, textএবং messageভেরিয়েবল একই পাঠ্য ধারণ করে এমন বিভিন্ন বস্তুর দিকে নির্দেশ করে।


4
কাজ
Java Syntax,  লেভেল 4পাঠ 6
লক করা আছে
Jen or Jen?
Jen, Company X's admin, learned how to pilot a space ship and flew away to another planet. People in Company X are good and sincere. It's just that they're scatterbrained and they mix up names. So they decided that the new administrator would also be called Jen. Let's help Company X find their Jen: write a program that checks the identity of two entered names.