P
P
pw0ned2018-09-10 00:42:24
C++ / C#
pw0ned, 2018-09-10 00:42:24

Trying to capitalize the last letter, how?

I'm trying to capitalize the last letter in a string.
Here's what happened at the moment.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace evilbeck
{
    class Program
    {
        static void Main(string[] args)
        {
            string file = @"2.txt";

            Console.WriteLine();
            Console.WriteLine($"Ваш файл: {file}");
            Console.WriteLine();

            string[] readfile = File.ReadAllLines(file);

            foreach (string s in readfile)
            {
                var tmp = file.ToCharArray();
                var last = tmp[tmp.Length - 1];
                file = file.Remove(file.Length - 1) + last.ToString().ToUpper();
                Console.WriteLine(file);
            }

            Console.ReadLine();
        }
    }
}

I do not understand why it does not read a text document, thereby displaying only 3 lines 2.txT.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny, 2018-09-10
@pw0ned

var path = "2.txt";
var lines = File.ReadAllLines(path);
var newLines = new List<string>();
foreach (var line in lines)
{
  newLines.Add(line.Remove(line.Length - 1) + line.Last().ToString().ToUpper());
}
File.WriteAllLines(path, newLines);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question