字符串连接后缺少字符

时间:2018-12-03 07:50:53

标签: c# linq

我遇到了一个问题,即位置(例如39)处的字母会替换为我想输入的文字。但是我想要的是在位置39插入文本,而不是替换它。任何人都可以指导我。

string description = variables[1]["value"].ToString();// where I get the text
int nInterval = 39;// for every 39 characters in the text I would have a newline
string res = String.Concat(description.Select((c, z) => z > 0 && (z % nInterval) == 0 ? Environment.NewLine +"Hello"+(((z/ nInterval)*18)+83).ToString()+"world": c.ToString()));
file_lines = file_lines.Replace("<<<terms_conditions>>>",resterms); //file_lines is where I read the text file 

原始文字

Present this redemption slip to receive: One

在String.Concat之后

Present this redemption slip to receive\r\n\u001bHello101world
One //: is gone 

我还有一个问题,如果文本中包含*,我想放一个新行。如果有人能够提供帮助,那就太好了。

编辑: 我想要实现的是这样的事情

输入

*Item is considered paid if unsealed.*No replacement or compensation will be given for any expired coupons.

所以像我需要查找每个39个字符,还要*输入换行符,这样就可以了

输出

*Item is considered paid if unsealed.  
*No replacement or compensation will be  
given for any expired coupons.

3 个答案:

答案 0 :(得分:0)

尝试String.Insert(Int32, String)方法

在需要换行的地方插入\n

答案 1 :(得分:0)

如果我正确理解了您的问题,那么您希望每39个字符后换行。您可以为此使用string.Insert(Int32,String)方法。

并使用String.Replace(String,String)解决您的*问题。

下面的代码段使用一个简单的for循环来实现。

        string sampleStr = "Lorem Ipsum* is simply..";

        for (int i = 39;  i < sampleStr.Length;  i = i + 39){
            sampleStr = sampleStr.Insert(i, Environment.NewLine);
        }

        //sampleStr = sampleStr.Replace("*", Environment.NewLine);

        int[] indexes = Enumerable.Range(0, sampleStr.Length).Where(x => sampleStr[x] == '*').ToArray();

        for (int i = 0; i < indexes.Length; i++)
        {
            int position = indexes[i];
            if (position > 0) sampleStr = sampleStr.Insert(position, Environment.NewLine);
        }

如果您想一起做

        int[] indexes = Enumerable.Range(0, sampleStr.Length).Where(x => sampleStr[x] == '*' || x % 39 == 0).ToArray();
        int j = 0;

        foreach (var position in indexes)
        {
            if (position > 0)
            {
                sampleStr = sampleStr.Insert(position + j, Environment.NewLine);
                j = j + 2; // increment by two since newline will take two chars 
            }
        }

答案 2 :(得分:0)

在不讨论选择哪种方法以获得理想结果的情况下,该代码的问题在于,在第39个字符处添加了一些文本,但该字符本身已被忘记。

更改下面的行应该可以得到预期的输出。

connectString = cx_Oracle.makedsn(IP, PORT, service_name=SERVICENAME)
connection = cx_Oracle.connect(username, password, connectString)

<==基于问题澄清的更新答案==>

我相信这会做您想要的。查看注释。

string res = String.Concat(description.Select((c, z) => z > 0 && (z % nInterval) == 0 ? Environment.NewLine + "Hello" + (((z / nInterval) * 18) + 83).ToString() + "world" + c.ToString() : c.ToString()));

输出:

        var description = "*Item is considered paid if unsealed.*No replacement or compensation will be given for any expired coupons.";

        var nInterval = 39; // for every 39 characters in the text I would have a newline
        var newline = "\r\n"; // for clarity in the Linq statement.  Can be set to Environment.Newline if desired.
        var z = 0; // we'll handle the count manually.

        var res = string.Concat(
            description.Select(
                (c) => (++z == nInterval || c == '*') // increment z and check if we've hit the boundary OR if we've hit a *
                    && ((z = 0)==0) // resetting the count - this only happens if the first condition was true 
                    ? newline + (c == ' ' ? string.Empty : c.ToString()) // if the first character of a newline is a space, we don't need it
                    : c.ToString()
                ));
相关问题