简体   繁体   English

按字符长度c#将字符串分为三部分

[英]Split string in three parts by character length c#

I've got a question about splitting a string message in three parts depending on the length of characters. 我有一个关于根据字符长度将字符串消息分为三部分的问题。 The reason is because my stored procedure won't take more than 32767 characters. 原因是因为我的存储过程最多可以容纳32767个字符。 (pl/sql payload) Therefore i would like to send three messages (three clobs) to the stored procedure which can append those messages and send it to a queue. (pl / sql有效负载)因此,我想向存储过程发送三个消息(三个Clob),该存储过程可以追加这些消息并将其发送到队列。

Which solution is the best if I've got a string message and I need to calculate it into three parts where the max length of the message can be 32.000 characters? 如果我有一个string message并且需要将其计算为三个部分(消息的最大长度可以为32.000个字符),哪种解决方案是最好的?

What the stored procedure need: (qname IN varchar2, i_clob1 IN clob, i_clob2 IN clob, i_clob3 IN clob) 存储过程需要什么: (qname IN varchar2, i_clob1 IN clob, i_clob2 IN clob, i_clob3 IN clob)

And how to send it in three parts if the string message is less than 32.000 characters for the first part but I want to send it in three parts anyway? 以及如果第一部分的string message少于32.000个字符,但我想分三部分发送,如何将其分为三部分发送?

Here is my code which take one message (i_clob). 这是我的代码,其中包含一条消息(i_clob)。

 public void Enqueue(string queueName, string mess)
        {
            OracleCommand cmd = null;
            try
            {
                cmd = new OracleCommand("", m_Connection)
                    {
                        CommandText = m_InSpName,
                        CommandType = CommandType.StoredProcedure
                    };


                //add Aq queue name 
                OracleParameter qName = new OracleParameter("qname", OracleType.VarChar)
                    {
                        Direction = ParameterDirection.Input,
                        Value = queueName
                    };

                //add message to enqueue
                OracleParameter message = new OracleParameter("i_clob", OracleType.Clob)
                    {
                        Direction = ParameterDirection.Input
                    };
                mess = mess.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");
                message.Value = mess;

                cmd.Parameters.Add(qName);
                cmd.Parameters.Add(message);

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //rethrow exception and make sure we clean up i.e. execute finally below
                throw new Exception("An error occurred trying to deliver the message to the queue", ex);
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                }
            }
        }

I understand that you are looking for something on these lines: 我了解您正在以下方面寻找东西:

string i_clob1 = "";
string i_clob2 = "";
string i_clob3 = "";
if (message.Length >= 3 && message.Length <= 32000 * 3)
{
    int lastStart = 2 * message.Length / 3;
    int lastLength = message.Length - lastStart;
    i_clob1 = message.Substring(0, message.Length / 3);
    i_clob2 = message.Substring(message.Length / 3, message.Length / 3);
    i_clob3 = message.Substring(lastStart, lastLength);
}
else if (message.Length < 3)
{
    i_clob1 = message;
}

Here is a generic String Splitter: 这是一个通用的字符串拆分器:

private IEnumerable<string> SplitString(string incomingString, int numberToCut)
    {
        int nombreDeCaractere = incomingString.Length;
        List<string> result = new List<string>();
        string temp = string.Empty;
        int curseur = 0;
        do
        {
            for (int i = 0; i < numberToCut - 1; i++)
            {
                temp += incomingString.Substring(i + curseur, 1);
            }

            result.Add(temp);
            temp = string.Empty;
            curseur += numberToCut;
        } while (nombreDeCaractere >= curseur + numberToCut);

        temp = string.Empty;
        for (int i = curseur; i < nombreDeCaractere; i++)
        {
            temp += incomingString.Substring(i, 1);
        }

        result.Add(temp);

        return result;
    }

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

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