How to convert text to utf-8
I have this line of code for an automatic Comma placer
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace Kommaplacer
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starter din kommasætning.. ");
string filePath = @"D:\Desktop\Personlige Dokumenter\PROGRAMMOER.NOOB\C++ ell. ANDET PROJEKTER\cskarp\Hello World\Hello World\Insert text\insert text here.txt";
try
{
byte[] bytes = File.ReadAllBytes(filePath);
string text = Encoding.UTF8.GetString(bytes);
string modifiedText = AddCommasBeforePrecedingWords(text);
modifiedText = AddSpaceAfterComma(modifiedText);
string modifiedFilePath = @"D:\Desktop\Personlige Dokumenter\PROGRAMMOER.NOOB\C++ ell. ANDET PROJEKTER\cskarp\Hello World\Hello World\Insert text\Modificeret_text.txt";
File.WriteAllText(modifiedFilePath, modifiedText, Encoding.UTF8); // Gem filen med UTF-8 encoding
Console.WriteLine("Kommasætningen er fuldført. Den modificerede tekst er gemt i Modificeret_text.txt.");
}
catch (Exception ex)
{
Console.WriteLine($"Fejl: {ex.Message}");
}
}
static string AddCommasBeforePrecedingWords(string text)
{
string pattern = @"\b(\S+)\s+(hv[a-zæøå]+)\b";
string replacement = "$1,$2";
string modifiedText = Regex.Replace(text, pattern, replacement, RegexOptions.IgnoreCase);
return modifiedText;
}
static string AddSpaceAfterComma(string text)
{
string modifiedText = text.Replace(",", ", ");
return modifiedText;
}
}
}
When I run the program it cannot write the comma-fied text in utf-8. Any suggestions on how to write it in utf-8?
I am using Visual Studio 20220 Replies