简体   繁体   English

如何替换包含未知值的字符串的一部分

[英]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.单词 Part 之前也会有一个制表符,在第一个 { 之后会有一个新行,在最后一个之前还会有一个新行和一个制表符。 How would I go about replacing the part inside the brackets or even all of it with another string.我将如何 go 将括号内的部分甚至全部替换为另一个字符串。

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}它输出: 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)特别是更多的'}'会使这个解决方案毫无价值......(而且它已经没有太大的价值了)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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