Multiline string literal in C#

后端 未结 13 1907
梦谈多话
梦谈多话 2020-11-22 11:15

Is there an easy way to create a multiline string literal in C#?

Here\'s what I have now:

string query = \"SELECT foo, bar\"
+ \" FROM table\"
+ \" W         


        
13条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 11:40

    You can use this two methods :

        private static String ReverseString(String str)
        {
            int word_length = 0;
            String result = "";
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == ' ')
                {
                    result = " " + result;
                    word_length = 0;
                }
                else
                {
                    result = result.Insert(word_length, str[i].ToString());
                    word_length++;
                }
            }
            return result;
        }
    //NASSIM LOUCHANI
        public static string SplitLineToMultiline(string input, int rowLength)
        {
            StringBuilder result = new StringBuilder();
            StringBuilder line = new StringBuilder();
    
            Stack stack = new Stack(ReverseString(input).Split(' '));
    
            while (stack.Count > 0)
            {
                var word = stack.Pop();
                if (word.Length > rowLength)
                {
                    string head = word.Substring(0, rowLength);
                    string tail = word.Substring(rowLength);
    
                    word = head;
                    stack.Push(tail);
                }
    
                if (line.Length + word.Length > rowLength)
                {
                    result.AppendLine(line.ToString());
                    line.Clear();
                }
    
                line.Append(word + " ");
            }
    
            result.Append(line);
            return result.ToString();
        }
    

    In the SplitLineToMultiline() , you need to define the string you want to use and the row length , it's very simple . Thank you .

提交回复
热议问题