Pobranie tekst z pliku, a następnie zamiana danej litery na inną.

0

Witam,
Probuję napisać program który pobierze tekst z pliku, a następnie zamięni wszystkie wystąpienia danej litery w tekscie.
Niestety nie wiem, jak podmienić konkretną literę .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void openBtn_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
            openFileDialog1.FileName = "Napisy";
            openFileDialog1.Filter = "Pliki tekstowe(*.txt)|*.txt";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog1.FileName;
                textBox2.Text= File.ReadAllText(fileName);
            }
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {

        }

        private void changeBtn_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(textBox2.Text))
            {
                string change = textBox2.Text;
                if ()
                {

                }
            }
            else
            {
                MessageBox.Show("Nie wczytałes tekstu do zmiany!", "Błąd");
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }
    }
}

 
1
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Replace(string path, char oldChar, char newChar)
    {
        File.WriteAllText(path, File.ReadAllText(path).Replace(oldChar, newChar));
    }

    public static void Main(string[] args)
    {
        try
        {
            Replace("test.txt", 'l', 'H');
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

Plik:

Hello World
Hello Programmers

Po zmianie:

HeHHo WorHd
HeHHo Programmers
0

Super, dzięki ;)

0

A jak wpisać do kontrolki textBox polskie znaki ?

0

No jak to jak? Normalnie przecież.

0
 
private void openBtn_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
            openFileDialog1.FileName = "Napisy";
            openFileDialog1.Filter = "Pliki tekstowe(*.txt)|*.txt";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog1.FileName;
                textBox1.Text = fileName; // kontrolka odpowiedzialna za wyswietlenie sciezki pliku na ekranie
                textBox2.Text= File.ReadAllText(fileName); //  tutaj wczytuje tekst z pliku
            }
        }

Przy użyciu tej procedury wczytuje mi jakieś dziwne znaczki ...
plik zawiera tylko cztery litery: ą,ś,ć,ż

0

Czy mógłby ktoś pomóc ? Jeżeli zadałem zle pytanie to napiszcie coś to poprawie...
Tak wygląda aktualnie kod i przy zamianie polskich znaków, w pliku pojawiają się dziwne znaczki .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public void ReplaceWords(string path, char oldChar, char newChar)
        {
            File.WriteAllText(path,File.ReadAllText(path).Replace(oldChar,newChar));
        }
        private void ChangePolishSign()
        {
            ReplaceWords(textBox1.Text, 'ą', 'a');
            ReplaceWords(textBox1.Text, 'ę', 'e');
            ReplaceWords(textBox1.Text, 'ś', 's');
            ReplaceWords(textBox1.Text, 'ż', 'z');
            ReplaceWords(textBox1.Text, 'ź', 'z');
            ReplaceWords(textBox1.Text, 'ć', 'c');
            ReplaceWords(textBox1.Text, 'ó', 'o');
            ReplaceWords(textBox1.Text, 'ł', 'l');
            ReplaceWords(textBox1.Text, 'ń', 'n');

            ReplaceWords(textBox1.Text, 'Ą', 'A');
            ReplaceWords(textBox1.Text, 'Ę', 'E');
            ReplaceWords(textBox1.Text, 'Ś', 'S');
            ReplaceWords(textBox1.Text, 'Ż', 'Z');
            ReplaceWords(textBox1.Text, 'Ź', 'Z');
            ReplaceWords(textBox1.Text, 'Ó', 'O');
            ReplaceWords(textBox1.Text, 'Ł', 'L');
            ReplaceWords(textBox1.Text, 'Ń', 'N');
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void openBtn_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            openFileDialog1.FileName = "Nazwa_pliku";
            openFileDialog1.Filter = "pliki srt (*.srt)|*.srt|pliki tekstowe (*.txt)|*.txt";
            
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog1.FileName;
                textBox1.Text = fileName; 
            }
        }

        private void changeBtn_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(textBox1.Text))
            {
                ChangePolishSign();
                MessageBox.Show("Zrobione!", "Done");
            }
            else
            {
                MessageBox.Show("Nie wybrałeś pliku!", "Błąd");
            } 
        }

       

     
    }
}

 
1

No nie wiem. Jak najbardziej powinno wszystko obsługiwać polskie znaki.:

using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

class Program
{
    [STAThread]
    public static void Main(string[] args)
    {
        var form = new Form();

        var oldChar = new TextBox();
        var newChar = new TextBox();
        var content = new RichTextBox();

        oldChar.MaxLength = newChar.MaxLength = 1;

        var load = new Button();
        load.Text = "Load";
        load.Click += (sender, a) =>
        {
            var openDialog = new OpenFileDialog();
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                content.Text = File.ReadAllText(openDialog.SafeFileName);
            }
        };

        var replace = new Button();
        replace.Text = "Replace";
        replace.Click += (sender, a) =>
        {
            if (!string.IsNullOrWhiteSpace(oldChar.Text) && !string.IsNullOrWhiteSpace(newChar.Text))
            {
                content.Text = content.Text.Replace(oldChar.Text[0], newChar.Text[0]);

                // Tak też zadziała tylko typem zmienianym będzie string, a nie char.
                // content.Text = content.Text.Replace(oldChar.Text, newChar.Text);
            }
            else MessageBox.Show("No chars to replace.");
        };

        var save = new Button();
        save.Text = "Save";
        save.Click += (sender, a) =>
        {
            var saveDialog = new SaveFileDialog();
            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllText(saveDialog.FileName, content.Text);
            }
        };

        var panel = new TableLayoutPanel();
        panel.Dock = DockStyle.Fill;
        panel.Controls.Add(oldChar);
        panel.Controls.Add(newChar);
        panel.Controls.Add(content);
        panel.Controls.Add(load);
        panel.Controls.Add(replace);
        panel.Controls.Add(save);

        form.Controls.Add(panel);
        form.ShowDialog();
    }
}

Nawet wywołanie gołej funkcji File.WriteAllText(path,File.ReadAllText(path).Replace(oldChar,newChar)); działa. C# koduje char na dwóch bajtach to nie powinno być problemu z ogonkami. Przynajmniej nigdy się z takowymi nie spotkałem.

0

Sprawdź formatowanie tego pliku tekstowego i czy czytasz i zapisujesz w tym samym formacie ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1