Android में स्ट्रिंग तुलना तकनीकें
यह उदाहरण दर्शाता है कि Android में स्ट्रिंग तुलना का उपयोग कैसे करें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: लेआउट XML जोड़ें (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginTop="100dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter string" />
<Button
android:id="@+id/click"
android:layout_marginTop="50dp"
style="@style/Base.TextAppearance.AppCompat.Widget.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:background="#c1c1c1"
android:textColor="#FFF"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
उपरोक्त कोड में, हमने EditText, बटन और TextViews लिए हैं। जब उपयोगकर्ता बटन पर क्लिक करता है, तो यह EditText से डेटा लेगा और स्ट्रिंग तुलना तकनीकों के साथ स्ट्रिंग में हेरफेर करेगा। हेरफेर किए गए डेटा का परिणाम TextViews में जोड़ा जाएगा।
चरण 3: MainActivity.java कोड जोड़ें (src/MainActivity.java)
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText edit_query;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_query = findViewById(R.id.edit_query);
final TextView textView = findViewById(R.id.text);
final TextView textView1 = findViewById(R.id.text1);
final TextView textView2 = findViewById(R.id.text2);
findViewById(R.id.click).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!edit_query.getText().toString().isEmpty()) {
String s1 = edit_query.getText().toString();
String s2 = edit_query.getText().toString();
String s3 = new String(edit_query.getText().toString());
boolean equals = s1.equals(s2);
textView.setText(" equals method " + equals);
boolean equals1 = s1 == s3;
textView1.setText(" == result " + equals1);
int equals2 = s1.compareTo(s2);
textView2.setText("compare to result is " + String.valueOf(equals2));
}
}
});
}
}
एप्लिकेशन चलाएं
अपने वास्तविक Android मोबाइल डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio से ऐप चलाने के लिए, अपनी प्रोजेक्ट की एक्टिविटी फ़ाइलों में से एक खोलें और टूलबार से Run आइकन पर क्लिक करें। अपने मोबाइल डिवाइस को एक विकल्प के रूप में चुनें और फिर अपने मोबाइल डिवाइस की जांच करें जो आपकी डिफ़ॉल्ट स्क्रीन प्रदर्शित करेगा -

उपरोक्त परिणाम में, यह स्ट्रिंग तुलना तकनीकों का परिणाम दिखा रहा है।