কম্পিউটার টিউটোরিয়াল

C#-এ লিস্ট থেকে খালি স্ট্রিং সরানো (RemoveAt)

List<string>.RemoveAt(index) মেথড ব্যবহার করে নির্দিষ্ট ইন্ডেক্সের এলিমেন্ট (খালি স্ট্রিং সহ) সরানো যায়। এটি লিস্টকে ইন-প্লেস মডিফাই করে।

সিনট্যাক্স


List<string> list = new List<string> { "", "", "" };
list.RemoveAt(0); // প্রথম খালি স্ট্রিং সরানো

সম্পূর্ণ উদাহরণ


using System;
using System.Collections.Generic;

class RemoveEmptyStringDemo
{
    static void Main()
    {
        List<string> myList = new List<string> { "", "", "" };
        
        Console.WriteLine("প্রাথমিক লিস্ট (খালি স্ট্রিংসহ):");
        PrintList(myList);
        
        // প্রথম খালি এলিমেন্ট সরান
        myList.RemoveAt(0);
        
        Console.WriteLine("\nRemoveAt(0) এর পর:");
        PrintList(myList);
        
        // সব খালি স্ট্রিং সরাতে (উত্তম পদ্ধতি)
        myList.RemoveAll(s => string.IsNullOrEmpty(s));
        Console.WriteLine("\nRemoveAll(IsNullOrEmpty) এর পর:");
        PrintList(myList);
    }
    
    static void PrintList(List<string> list)
    {
        for (int i = 0; i < list.Count; i++)
            Console.WriteLine($"  [{i}] = '{list[i]}' (Length: {list[i].Length})");
    }
}

আউটপুট


প্রাথমিক লিস্ট (খালি স্ট্রিংসহ):
  [0] = '' (Length: 0)
  [1] = '' (Length: 0)
  [2] = '' (Length: 0)

RemoveAt(0) এর পর:
  [0] = '' (Length: 0)
  [1] = '' (Length: 0)

RemoveAll(IsNullOrEmpty) এর পর:
  (লিস্ট খালি)

সর্বোত্তম পদ্ধতি: সব খালি/হোয়াইটস্পেস স্ট্রিং সরানো


// শুধু খালি স্ট্রিং
list.RemoveAll(s => s == "");

// খালি বা হোয়াইটস্পেস-ওনলি
list.RemoveAll(s => string.IsNullOrWhiteSpace(s));

LINQ পদ্ধতি (নতুন লিস্ট তৈরি)


var nonEmpty = list.Where(s => !string.IsNullOrEmpty(s)).ToList();
// অথবা
var nonEmpty = list.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();

তুলনা

পদ্ধতিইন-প্লেস?শর্ত
RemoveAt(index)হ্যাঁনির্দিষ্ট ইন্ডেক্স
Remove(item)হ্যাঁপ্রথম মিল
RemoveAll(predicate)হ্যাঁসব মিলে যাওয়া
Where(...).ToList()না (নতুন লিস্ট)ফিল্টার
  1. Microsoft Outlook-এ서 Boxbe Waiting List সরানোর পদ্ধতি

  2. পাইথনে ইনডেক্স দিয়ে লিস্ট থেকে এলিমেন্ট রিমুভ কীভাবে করবেন?

  3. পাইথনে স্ট্রিং এবং স্ট্রিং লিস্ট থেকে টাপল কীভাবে তৈরি করবেন?

  4. পাইথনে লিস্ট থেকে একটি অবজেক্ট রিমুভ করার পদ্ধতি