ধরুন আমাদের নিচের স্ট্রিং থেকে লাইন ব্রেক, স্পেস এবং ট্যাব স্পেস এলিমিনেট করতে হবে।
Replace() মেথড ব্যবহার করে উদাহরণ
আমরা স্ট্রিংর `Replace()` এক্সটেনশন মেথড ব্যবহার করে এটি করতে পারি।
using System;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
string testString = "Hello \n\r beautiful \n\t world";
string replacedValue = testString.Replace("\n\r", "_").Replace("\n\t", "_");
Console.WriteLine(replacedValue);
Console.ReadLine();
}
}
}
আউটপুট
Hello _ beautiful _ world
Regex ব্যবহার করে উদাহরণ
আমরা একই অপারেশন করার জন্য `Regex`-ও ব্যবহার করতে পারি। `Regex` `System.Text.RegularExpressions` নেমস্পেসে উপলব্ধ।
using System;
using System.Text.RegularExpressions;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
string testString = "Hello \n\r beautiful \n\t world";
string replacedValue = Regex.Replace(testString, @"\n\r|\n\t", "_");
Console.WriteLine(replacedValue);
Console.ReadLine();
}
}
}
আউটপুট
Hello _ beautiful _ world