6

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] )

3
  • 4
    Please could you give an example of the input and desired output, then I might be able to understand what you need to do. Commented Mar 29, 2011 at 9:29
  • Cant clearly understand your problem, but why you didn't add extra spaces before and after brackets in code. Replace "[" with " [" and so on. Commented Mar 29, 2011 at 9:57
  • User will be defining the brackets while creating an expression which is a textbox. Commented Mar 29, 2011 at 10:43

4 Answers 4

10

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).

Sign up to request clarification or add additional context in comments.

Comments

3

You can do that with regular expressions:

using System.Text.RegularExpressions;
class Program {
    static void Main() {
        string expression = "(UI$.SlNo-UI+UI$.Task)-(UI$.Responsible_Person*UI$.StartDate) ";
        string replaced = Regex.Replace(expression, @"([\w\$\.]+)", " [ $1 ] ");
    }
}

If you are not familiar with regular expressions this might look rather cryptic, but they are a powerful tool, and worth learning. In case, you may check how regular expressions work, and use a tool like Expresso to test your regular expressions.

Hope this helps...

Comments

1

Here is an algorithm that does not use regular expressions.

//Applies dobule spacing between characters
public static string DoubleSpace(string s)
{
    if (string.IsNullOrEmpty(s))
    {
        return string.Empty;
    }

    char[] a = s.ToCharArray();
    char[] b = new char[ (a.Length * 2) - 1];

    int bIndex = 0;
    for(int i = 0; i < a.Length; i++)
    {
        b[bIndex++] = a[i];

        //Insert a white space after the char
        if(i < (a.Length - 1))
        {
            b[bIndex++] = ' ';
        }
    }

    return new string(b);
}

Comments

0

Well, you can do this by using Regular expressions, search for specific paterns and add brackets where needed. You could also simply Replace every Paranthesis with the same Paranthesis but with spaces on each end.

I would also advice you to use StringBuilder aswell instead of appending to an existing string (this creates a new string for each manipulation, StringBuilder has a smaller memory footprint when doing this kind of manipulation)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.