Removing numeration in .srt file using C# - c#

I am an amateur in coding and I'm struggling with a simple problem. I need to remove the numeration in the .srt file, but I couldn't find the correct regex, using regex might be a bad idea, because the subtitle itself can contain a number, I was thinking about matching "-->" and then removing the previous line, but I couldn't do it. Can someone help me?
I need to remove the following lines (numeration)
screenshot
UPDATE
I came up with the following code, which replaces numbers with spaces, but it doesn't delete the line, as I want
var source = File.ReadAllLines("C:\\Users\\name\\Desktop\\temp1\\Doctor.Strange.2016.720p.BluRay.x264.[YTS.MX]-English.srt");
var newFile = #"C:\Users\name\Desktop\temp1\new.srt";
for (int i = 0; i < source.Length; i++)
{
if (source[i].Contains(matchSymbol))
{
source[i - 1] = "";
}
}
File.WriteAllLines(newFile, source);

If the file is not super large, a better way would be to read every line of the file and check each line with Int32.TryParse(). If True is returned for that line, skip it, otherwise write the contents of the line to an output file.

Something like this should work:
void RemoveNumberLines()
{
var source = File.ReadAllLines("C:\\Users\\name\\Desktop\\temp1\\Doctor.Strange.2016.720p.BluRay.x264.[YTS.MX]-English.srt");
var newFile = #"C:\Users\name\Desktop\temp1\new.srt";
var matchSymbol = "-->";
var myNewFile = new List<string>(); bool addPrevious = true; string previous = "";
foreach (var item in source)
{
addPrevious = !item.Contains(matchSymbol);
if (addPrevious)
{
myNewFile.Add(previous);
}
previous = item;
}
if (addPrevious)
{
myNewFile.Add(previous);
}
File.WriteAllLines(newFile, myNewFile);
}
Adding the previous line, if the active line won't contain "-->".

Related

how can i search a file find a string and get all the text between / /

ok i trying my best to get this string
"/i/models/fc6d6067621442ebb6f4238636363d47/textures?optimized=1"
but i need to get the jumble of numbers and between the // only and save it to a string because each json file i open has a different amount of numbers
i tried using regex but it finds different concordances in the file and i only need the one that matches this line
the file is a json file
https://i.imgur.com/B98gdBD.png
but not sure how to accomplish this
this is the code i tried
List<string> found = new List<string>();
string line;
using (StreamReader file = new StreamReader(Application.StartupPath + "\\textures.json"))
{
while ((line = file.ReadLine()) != null)
{
if (line.Contains("textures?optimized=1"))
{
texturemodel = line;
}
}
}
this code does work and does get the line needed only issue i have with it it has ;} on the end of it so its nearly working but not sure how to remove the white space at the start and the ;{ at the end of the file the white space is at the beginning of the file
thanks in advance elfenliedtopfan5
if you are sure that that would always be the format, try string.Split()
var stringWithJumbledNumbersAndLetters = "/i/models/fc6d6067621442ebb6f4238636363d47/textures?optimized=1";
var splitString = stringWithJumbledNumbersAndLetters.Split('/');
//splitString[0] = i;
//splitString[1] = models;
//splitString[2] = fc6d6067621442ebb6f4238636363d47;
//splitString[3] = textures?optimized=1;
return splitString[2];
or direct would be
var stringWithJumbledNumbersAndLetters = "/i/models/fc6d6067621442ebb6f4238636363d47/textures?optimized=1";
return stringWithJumbledNumbersAndLetters.Split('/')[2];
String s = "/i/models/fc6d6067621442ebb6f4238636363d47/textures?optimized=1";
String[] result = s.split("/");
System.out.println(result[3]);

C# Parsing a text file misses the first line of every text file

I am making a number to music note value converter from text files and I am having trouble parsing text files in my C# code. The stream reader or parser seems to ignore every first line of text from different text files, I am learning from parsing CSV and assumed that I can parse text files the same way that I can parse CSV files. Here is my code:
static List<NoteColumns> ReadNoteValues(string fileName)
{
var allNotes = new List<NoteColumns>();
using (var reader = new StreamReader(fileName))
{
string line = "";
reader.ReadLine();
while ((line = reader.ReadLine()) != null)
{
string[] cols = line.Split('|');
var noteColumn = new NoteColumns();
if (line.StartsWith("-"))
{
continue;
}
double col;
if (double.TryParse(cols[0], out col))
{
noteColumn.QCol = col;
}
if (double.TryParse(cols[1], out col))
{
noteColumn.WCol = col;
}
}
}
return allNotes;
}
Here are the first 4 lines of one of my text file:
0.1|0.0|
0.0|0.1|
0.3|0.0|
0.1|0.0|
So every time that I have an output it will always skip the first line and move onto the next line. After it misses the first line it converts all the other values perfectly.
I have tried using a foreach loop but I end up getting an 'Index Out Of Range Exception'. Am I missing something/being stupid? Thanks for your time
You have a call to reader.ReadLine(); before your loop, which you don't assign to anything
It's because you have already added ReadLine() before the while loop which skips the first line
In order to avoid such errors (erroneous reader.ReadLine() in the beginning) get rid of Readers at all and query file with Linq (let .Net open and close the file, read it line after line and do all low level work for you):
using System.IO;
using System.Linq;
...
static List<NoteColumns> ReadNoteValues(string fileName) {
return File
.ReadLines(fileName)
.Where(line => !line.StartsWith('-'))
.Select(line => line.Split('|'))
.Select(cols => {
var noteColumn = new NoteColumns();
if (double.TryParse(cols[0], out var col))
noteColumn.QCol = col;
if (double.TryParse(cols[1], out var col))
noteColumn.WCol = col;
return noteColumn;
})
.ToList();
}

copy lines into new file except specific line from text file in c#

I'm trying to copy data from textfile1 to a new textfile (textfile2). but there's a specific line that i wish to exclude. i found a way to get the line where data i want to exclude is from.
What i'm doing here is that i'm checking if "testfile.txt" contains the string inputted by the user from textbox1. if the file does contain said input, it would then check if that same input is also available in "textfile1".
string old = #"textfile1.txt";
string new = #"textfile2.txt";
string test = #"testfile.txt";
string[] line = File.ReadAllLines(test);
for (int i = 0; i < line.Length; i++)
{
if (line[i].Contains(textbox1.Text))
{
string fileData = File.ReadAllText(path);
for (int n = 0; n < line.Length; n++)
{
if (line[n].Contains(textbox1.Text))
{
n++; //gets line number
//other code
}
}
}
}
now what i want to do is to copy everything in that textfile1 except the line wherein the inputted data was found.
SAMPLE DATA
textfile1.txt (id,item,price)
0001,apple,5
0002,banana,3
0003,mango,6
user inputs 0002 in textbox1
SUPPOSED OUTPUT
textfile2.txt
0001,apple,5
0003,mango,6
"why not just delete that line from the file?" well, i tried but yeah i'm having troubles with is too so i tried doing this instead.
I've been trying all sorts of things but i can't seem to make anything work :( any suggestions? any form of help would be appreciated!
You've got too many loops. You should be able to do it in one:
string oldFile = #"textfile1.txt";
string newFile = #"textfile2.txt";
string test = #"testfile.txt";
string[] lines = File.ReadAllLines(oldFile);
using (StreamWriter w = File.AppendText(newFile))
{
foreach(var line in lines) {
if (!line.Contains(textbox1.Text))
{
w.WriteLine(line);
}
}
}
Alternatively, you could also use LINQ to filter out the lines you don't want, and then write them back out all at once:
var newLines = File.ReadLines(oldFile).Where(l => !l.contains(textbox1.Text));
File.WriteAllLines(newFile, newLines);

How to delete all match and delete text

How can I delete words from a "string" in the RichTextBox.
Example:
[02/04/2014 17:04:21] Thread 1 Banned: xxxxxxxxx#xxxx.tld
[02/04/2014 17:04:21] Thread 2: Banned: xxxxxxxxx#xxxx.tld
[02/04/2014 17:04:21] Thread 3: Banned: xxxxxxxxx#xxxx.tld
[02/04/2014 17:04:21] Thread 4: Banned: xxxxxxxxx#xxxx.tld
I would like to delete all rows with the word "Banned" in the line.
How can I do this?
Thanks in advance.
You can use LINQ to remove all the lines that contains the work "Banned":
richTextBox1.Lines = richTextBox1.Lines
.Where((line, b) => !line.Contains("Banned"))
.Select((line, b) => line).ToArray();
I know this method looks ugly. But, if you don't want to remove formatting from the existing text in the richtextbox then you should use this method. This example is not tested but, you can get logic from here.
for (int iLine = 0; iLine < rtf.Lines.Length; iLine++)
{
if (rtf.Lines[iLine].Contains("Banned"))
{
int iIndex = rtf.Text.IndexOf(rtf.Lines[iLine]);
rtf.SelectionStart = iIndex;
rtf.SelectionLength = rtf.Lines[iLine].Length;
rtf.SelectedText = string.Empty;
iLine--; //-- is beacause you are removing a line from the Lines array.
}
}
You could try use the answer from this post - I would adjust the code slightly to neaten it up a bit.
URL:
what is the best way to remove words from richtextbox?
Code snippet from the URL (which would need to be tidied up).
string[] lines = richTextBox1.Lines;
List<string> linesToAdd = new List<string>();
string filterString = "Banned".";
foreach (string s in lines)
{
string temp = s;
if (s.Contains(filterString))
temp = s.Replace(filterString, string.Empty);
linesToAdd.Add(temp);
}
richTextBox1.Lines = linesToAdd.ToArray();
I would adjust the above code and whilst still using the loop, just check if the line contains the word you looking for "Banned" and then remove the line / do what is need with it.
I hope this helps?

Search for a keyword in C# and output the line as a string

How would it be possible to search for a string e.g. #Test1 in a text file and then output the line below it as a string e.g.
Test.txt
#Test1
86/100
#Test2
99/100
#Test3
13/100
so if #Test2 was the search keyword "99/200" would be turned into a string
Parse the file once, store the results in a dictionary. Then lookup in the dictionary.
var dictionary = new Dictionary<string, string>();
var lines = File.ReadLines("testScores.txt");
var e = lines.GetEnumerator();
while(e.MoveNext()) {
if(e.Current.StartsWith("#Test")) {
string test = e.Current;
if(e.MoveNext()) {
dictionary.Add(test, e.Current);
}
else {
throw new Exception("File not in expected format.");
}
}
}
Now you can just say
Console.WriteLine(dictionary["#Test1"]);
etc.
Also, long-term, I recommend moving to a database.
Use readline and search for the string (ex. #Test1) and then use the next line as input.
If the exactly above is the file format. Then you can use this
1. read all lines till eof in an array.
2. now run a loop and check if the string[] is not empty.
Hold the value in some other array or list.
now you have items one after one. so whenever you use loop and use [i][i+1],
it will give you the test number and score.
Hope this might help.
How about RegularExpressions? here's a good example
This should do it for you:
int lineCounter = 0;
StreamReader strReader = new StreamReader(path);
while (!strReader.EndOfStream)
{
string fileLine = strReader.ReadLine();
if (Regex.IsMatch(fileLine,pattern))
{
Console.WriteLine(pattern + "found in line " +lineCounter.ToString());
}
lineCounter++;
}

Categories