Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert spaces between the characters of a string

Tags:

c#

Is there an easy method to insert spaces between the characters of a string? I'm using the below code which takes a string (for example ( UI$.EmployeeHours * UI.DailySalary ) / ( Month ) ) . As this information is getting from an excel sheet, i need to insert [] for each columnname. The issue occurs if user avoids giving spaces after each paranthesis as well as an operator. AnyOne to help?

      text = e.Expression.Split(Splitter);
      string expressionString = null;
      for (int temp = 0; temp < text.Length; temp++)
                        {
                            string str = null;
                            str = text[temp];
                            if (str.Length != 1 && str != "")
                            {
                                expressionString = expressionString + "[" + text[temp].TrimEnd() + "]";
                            }
                            else
                                expressionString = expressionString + str;

                        }

User might be inputing something like (UI$.SlNo-UI+UI$.Task)-(UI$.Responsible_Person*UI$.StartDate) while my desired output is ( [UI$.SlNo-UI] + [UI$.Task] ) - ([UI$.Responsible_Person] * [UI$.StartDate] )

like image 612
NewBie Avatar asked Dec 17 '22 15:12

NewBie


1 Answers

Here is a short way to insert spaces after every single character in a string (which I know isn't exactly what you were asking for):

var withSpaces = withoutSpaces.Aggregate(string.Empty, (c, i) => c + i + ' ');

This generates a string the same as the first, except with a space after each character (including the last character).

like image 79
Manus Hand Avatar answered Jan 12 '23 10:01

Manus Hand