How to add Text to a specific Line of a Document
is it possible to add just a little text into a specific Line (a Number) of a text. If yes, how? thanks in advance
15 Replies
What have you tried so far? @Daiko Games
not much this : File.ReadAllLines(JSON).Skip(LineWhereToDoTheSign - 1).Take(1).First().Append(LineWhereToDoTheSign); I know it is completeley wrong
I just don´t understand how to append text into a specific line - do you know it?
Well, you’ve got the rough idea already.
Grab the first N lines, insert yours, grab the rest.
Could you pls. show me an example, thx
I am not really long into coding, just a year and I have never asked myself that question
Does the file store json
yes, but does it matter?
i don´t think it matters...
If it contains json then it might be easier to parse it into a data structure, modify it, then serialize it back into json and overwrite the file.
I don´t want to do that, it is just extra steps, isn´t there an easier way?
or might it be the easier thing to do?
is it the easier way
??
@Daiko Games can you provide some context as to the purpose of the file and why you want to insert text into it?
As the others have said, it might be simpler and safer to choose a different approach to solve your problem.
It is not really json it is just some plain text…
You were asked “Does the file store json” and replied “yes, but does it matter?”
So, which is it?
😦 I said something wrong at the beginning sorry
Okay so, what part do you need help with?
Okay, so to summarize:
You're trying to insert a line of text at a specific line into a text file that is not in json format.
A simple (not most efficient) way could be:
Notes:
- I use
ToList
to get access to the Insert
method. This causes lots of copies to happen in the background, could be fine for your use though.
- lineIndex
is the zero-based index of the line you wish to insert.
- line
is the line string you want to insertThat is right
I will test it out thanks ☺️