简体   繁体   中英

How would I replace a part of a string that contains unknown values

So i want to replace some a part of a text file knowing only that it looks like this:

Part "name" {11.015586 1.208383 -5.521754 0.001870 -0.975887 0.218267 0xffffffff 0.133086 0.811246 0.000000 1.000000}

name would be inputted from terminal and inside of brackets would have hundreds of lines of values as well as other brackets inside it. Also the word Part would have a tab before it, there would be a new line after the first { before last one there also would be a new line and a tab. How would I go about replacing the part inside the brackets or even all of it with another string.

A solution could be:

            string unknownPieceOfText = "Part \"name\" {11.015586 1.208383 -5.521754 0.001870 -0.975887 0.218267 0xffffffff 0.133086 0.811246 0.000000 1.000000}";
            string anotherString = "another string";

            int OpeningAccolade = unknownPieceOfText.IndexOf('{');
            int ClosingAccolade = unknownPieceOfText.IndexOf('}');

            string newString = unknownPieceOfText.Substring(0, OpeningAccolade + 1) + anotherString + unknownPieceOfText.Substring(ClosingAccolade);

            Console.WriteLine(newString);

It outputs: Part "name" {another string}

But we do not know if there are more '{' or '}' inside that text. Especially more '}' will make this solution worthless... (and it already has not much value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM