কম্পিউটার

জাভা স্ট্রিং রিপ্লেস(), রিপ্লেস ফার্স্ট() এবং রিপ্লেস অল() মেথড


প্রতিস্থাপন() পদ্ধতি

প্রতিস্থাপন() স্ট্রিং ক্লাসের পদ্ধতি দুটি স্ট্রিং মান −

গ্রহণ করে
  • প্রতিস্থাপন করা স্ট্রিং (সাবস্ট্রিং) এর অংশকে প্রতিনিধিত্ব করে।

  • আরেকটি স্ট্রিং প্রতিনিধিত্ব করে যার সাথে আপনাকে নির্দিষ্ট সাবস্ট্রিং প্রতিস্থাপন করতে হবে।

এই পদ্ধতিটি ব্যবহার করে, আপনি জাভাতে একটি স্ট্রিংয়ের একটি অংশ প্রতিস্থাপন করতে পারেন।

উদাহরণ

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReplaceExample {
   public static void main(String args[]) throws FileNotFoundException {
      String filePath = "D://input.txt";
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      String input;
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input);
      }
      String contents = sb.toString();
      System.out.println("Contents of the file: \n"+contents);
      System.out.println(" ");
      //Replacing the word with desired one
      contents = contents.replace("Tutorialspoint", "TP");
      System.out.println("Contents of the file after replacing the desired word: \n"+contents);
   }
}

আউটপুট

Contents of the file:
Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free.

Contents of the file after replacing the desired word:
Hello how are you welcome to TP. At TP provide hundreds of technical tutorials for free.

এই পদ্ধতির আরেকটি বৈকল্পিকও দুটি অক্ষর গ্রহণ করে, যথাক্রমে বিদ্যমান অক্ষর এবং একটি অক্ষরকে প্রতিনিধিত্ব করে (একই ক্রমে) এবং পুরো স্ট্রিংটিতে পুরানো অক্ষরটিকে নতুন একটি দিয়ে প্রতিস্থাপন করে।

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReplaceExample {
   public static void main(String args[]) throws FileNotFoundException {
      String filePath = "D://input.txt";
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      String input;
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input);
      }
      String contents = sb.toString();
      System.out.println("Contents of the file: \n"+contents);
      System.out.println(" ");
      //Replacing the word with desired one
      contents = contents.replace('T', '#');
      System.out.println("Contents of the file after replacing the desired word: \n"+contents);
   }
}

আউটপুট

Contents of the file:
Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free.

Contents of the file after replacing the desired word:
Hello how are you welcome to #utorialspoint. At #utorialspoint provide hundreds of technical tutorials for free.

প্রতিস্থাপন সমস্ত() পদ্ধতি

প্রতিস্থাপন করুন() স্ট্রিং ক্লাসের পদ্ধতিটি একটি রেগুলার এক্সপ্রেশন এবং একটি প্রতিস্থাপন প্যাটার্ন/স্ট্রিং প্রতিনিধিত্বকারী দুটি স্ট্রিং গ্রহণ করে এবং নির্দিষ্ট প্যাটার্ন/স্ট্রিং দিয়ে মিলিত মানগুলি প্রতিস্থাপন করে।

উদাহরণ

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class replaceAllExample {
   public static void main(String args[]) throws FileNotFoundException {
      String filePath = "D://input.txt";
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      String input;
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input);
      }
      String contents = sb.toString();
      System.out.println("Contents of the file: \n"+contents);
      System.out.println();
      //Replacing the word with desired one
      contents = contents.replaceAll("\\bTutorialspoint\\b", "TP");
      System.out.println("Contents of the file after replacing the desired word: \n"+contents);
   }
}

আউটপুট

Contents of the file:
Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free.

Contents of the file after replacing the desired word:
Hello how are you welcome to TP. At TP provide hundreds of technical tutorials for free.

প্রতিস্থাপন ফার্স্ট() পদ্ধতি

প্রতিস্থাপন ফার্স্ট() স্ট্রিং ক্লাসের মেথড (এছাড়াও) রিপ্লেস ফার্স্ট একটি রেগুলার এক্সপ্রেশন এবং রিপ্লেসমেন্ট স্ট্রিং প্রতিনিধিত্বকারী দুটি স্ট্রিং গ্রহণ করে এবং প্রতিস্থাপন স্ট্রিং দিয়ে প্রথম ম্যাচটিকে প্রতিস্থাপন করে।

উদাহরণ

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReplaceExample {
   public static void main(String args[]) throws FileNotFoundException {
      String filePath = "D://input.txt";
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      String input;
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input);
      }
      String contents = sb.toString();
      System.out.println("Contents of the file: \n"+contents);
      System.out.println(" ");
      //Replacing the word with desired one
      contents = contents.replaceFirst("Tutorialspoint", "TP");
      System.out.println("Contents of the file after replacing the desired word: \n"+contents);
   }
}

আউটপুট

Contents of the file:
Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free.

Contents of the file after replacing the desired word:
Hello how are you welcome to TP. At Tutorialspoint provide hundreds of technical tutorials for free.

দ্রষ্টব্য − এই সমস্ত পদ্ধতিগুলি কেস সংবেদনশীল৷


  1. জাভাতে StringIndexOutOfBoundsException কি?

  2. কিভাবে আমরা জাভাতে পূর্বনির্ধারিত পদ্ধতি ব্যবহার না করে একটি স্ট্রিং সাজাতে পারি?

  3. জাভা ব্যবহার করে একটি JSON ফাইলের বিষয়বস্তু কিভাবে পড়তে হয়?

  4. ইন্টারফেসে জাভা 8 স্ট্যাটিক পদ্ধতি