0

I have a text file(.txt) like this

Dialogue: 0,0:00:01.99,0:00:03.84,Default,,0000,0000,0000,,Up you go!

Dialogue: 0,0:00:03.84,0:00:06.31,Default,,0000,0000,0000,,And, again!

Dialogue: 0,0:01:42.92,0:01:44.91,Default,,0000,0000,0000,,Hang on a minute.

I want to insert "{" and "}" into that file like this

Dialogue: 0,0:00:01.99,0:00:03.84,Default,,0000,0000,0000,,{Up you go!}

Dialogue: 0,0:00:03.84,0:00:06.31,Default,,0000,0000,0000,,{And, again!}

Dialogue: 0,0:01:42.92,0:01:44.91,Default,,0000,0000,0000,,{Hang on a minute.}

I mananed to insert "}" to the last using these code

            String path = openFileDialog1.FileName;
            List<string> line = new List<string>();
            StreamReader sr = new StreamReader(path);
            StringBuilder sb = new StringBuilder();
            String lines;

            while ((lines = sr.ReadLine()) != null)
            {
                line.Add(lines+"}");
            }

            foreach (string s in line)
            {
                sb.AppendLine(s);
            }

I want to insert "{" right after "0000,," . Sorry for my Eng and Thank you for your help.

4
  • Will there always be "0000,," in each line? Commented Jul 4, 2012 at 6:09
  • Yes MatthewRz. Every line in that text :D Commented Jul 4, 2012 at 6:12
  • @user1310506, will the text may contain "0000,," ever ? Commented Jul 4, 2012 at 6:25
  • @user1310506, I meant text after 0000,, may contain the same characters ? Commented Jul 4, 2012 at 6:28

5 Answers 5

1

Assuming your format includes 9 comma delimited sections before the dialog:

string InsertBrackets(string input)
{
    var idx = -1;
    for (int i = 0; i < 9; i++)
        idx = input.IndexOf(",", idx + 1);

    return input.Insert(idx + 1, "{") + "}";
}

You could also pass in the number of sections as a parameter instead of hardcoding to be 9, if you want to. And add some error checking, of course.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was about to write.
1
        while ((lines = sr.ReadLine()) != null)
        {
           var index = line.LastIndesOf(",,") + 2;
           lines = lines.Insert(index, "{"); 

           line.Add(lines+"}");                
        }   

4 Comments

Would fail on "And, again!", because of the comma in the text.
You have a comma in the string. I believe the resulting string will be 1Dialogue: 0,0:00:03.84,0:00:06.31,Default,,0000,0000,0000,,And,{ again!} instead of the one expected
I change i little int index = lines.LastIndexOf(",,"); lines = lines.Insert(index+2, "{"); line.Add(lines+"}");
A text containing two commas (e.g. a typo or other kind of rubish) would still break it.
0

It seems like some sort of subtitle.

In that case, you can just count the number of , you have encountered. And the rest are text dialogue, which you can surround with {}.

I don't write in C#, but there should be a function that allows you to split the String by regular expression. Just use it to split along the commas ,, and specify a limit to the number of splits (in this case, the number of delimiters is 9). Then pick the last token to surround with {}, and join the string back.

Comments

0

Here is a working example in ideone.

In short this is the function that adds the braces(This solution depends on the facts you always have same number of fields separated by commas and only last field may have commas):

   public static string AddBraces(string u) {
            string[] words = u.Split(',');
            string res = words[0];
            words[9] = "{" + words[9];
            for (int i = 1; i < words.Length;++i) {
                    res = res + ',' + words[i];
            }
            res = res + "}";
            return res;
    }

Comments

0

Assuming you always have two commas just before the text you would like to change (which, from your comment seems to be the case), you can try out something like this:

        String str = "Dialogue: 0,0:00:01.99,0:00:03.84,Default,,0000,0000,0000,,Up you go!";
        String newStr = Regex.Replace(str, "(.*,,)(.*)$", "$1{$2}");
        System.Console.WriteLine(str);
        System.Console.WriteLine(newStr);
        System.Console.ReadKey();

This yields the following:

Dialogue: 0,0:00:01.99,0:00:03.84,Default,,0000,0000,0000,,Up you go!
Dialogue: 0,0:00:01.99,0:00:03.84,Default,,0000,0000,0000,,{Up you go!}

This regular expression: (.*,,)(.*)$ will match two groups. The first group will contain the start of the string till the last two commas. The second group will contain the text after the last two commas and keep matching till the end of the string.

The "$1{$2}" section will replace the given string with the first matched group, followed by a {, then followed by the second matched group which is then followed by a }.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.