Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड टेक्स्टव्यू में concat() का उपयोग कैसे करें?

एंड्रॉइड में String.concat() से टेक्स्ट जोड़ना

यह ट्यूटोरियल दिखाता है कि EditText से इनपुट लेकर String.concat() विधि से टेक्स्ट जोड़कर TextView में कैसे प्रदर्शित करें।

चरण 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="नाम दर्ज करें"
        android:layout_height="wrap_content"
        android:inputType="textPersonName" />
    <Button
        android:id="@+id/click"
        android:text="जोड़ें और दिखाएं"
        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="20sp"
        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().trim();
                
                if (!input.isEmpty()) {
                    // concat() विधि उपयोग करके स्ट्रिंग जोड़ें
                    String result = input.concat(" tutorialspoint.com");
                    textView.setText(result);
                } else {
                    nameEditText.setError("कृपया नाम दर्ज करें");
                }
            }
        });
    }
}

concat() बनाम अन्य तरीके

विधिउदाहरणनल सेफ्टीप्रदर्शन
concat()a.concat(b)नल पर NPEअच्छा (सिंगल)
+ ऑपरेटरa + bनल सेफ ("null" स्ट्रिंग)अच्छा (कंपाइलर ऑप्टिमाइज़)
StringBuildernew StringBuilder().append(a).append(b)नल सेफबेस्ट (लूप/मल्टीपल)
String.format()String.format("%s %s", a, b)नल सेफधीमा
TextUtils.join()TextUtils.join("", a, b)नल सेफअच्छा

बेहतर दृष्टिकोण (नल-सेफ)

// नल चेक के साथ
String suffix = " tutorialspoint.com";
String result = (input != null ? input : "").concat(suffix);

// या + ऑपरेटर (अनुशंसित)
String result = input + suffix;

// Kotlin में
val result = "$input tutorialspoint.com"
// या
val result = input.plus(" tutorialspoint.com")

StringBuilder उदाहरण (एकाधिक जोड़ के लिए)

StringBuilder sb = new StringBuilder();
sb.append(input);
sb.append(" ");
sb.append("tutorialspoint.com");
sb.append(" - ");
sb.append("Android Tutorial");
textView.setText(sb.toString());

ऐप टेस्ट करें

नाम दर्ज करें (जैसे "सैराम"), बटन दबाएं - "सैराम tutorialspoint.com" दिखेगा।

  1. Android TextView में startsWith() का उपयोग कैसे करें? Android TextView में startsWith() का उपयोग कैसे करें?

    Android TextView में startsWith() का उपयोग यह उदाहरण दर्शाता है कि Android TextView में startsWith() मेथड का उपयोग करके जांचा जाता है कि स्ट्रिंग किसी विशेष प्रीफिक्स से शुरू होती है या नहीं। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजे

  1. Android TextView में split() का उपयोग कैसे करें? Android TextView में split() का उपयोग कैसे करें?

    Android TextView में split() का उपयोग यह उदाहरण दर्शाता है कि Android TextView में split() मेथड का उपयोग करके स्ट्रिंग को रेगुलर एक्सप्रेशन के आधार पर कैसे विभाजित किया जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं। चर

  1. Android TextView में lastIndexOf() का उपयोग कैसे करें? Android TextView में lastIndexOf() का उपयोग कैसे करें?

    Android TextView में lastIndexOf() का उपयोग यह उदाहरण दर्शाता है कि Android TextView में lastIndexOf() मेथड का उपयोग करके किसी सबस्ट्रिंग का अंतिम इंडेक्स कैसे ढूंढा जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं। चरण 2