开发者

How to insert spaces between the characters of a string

开发者 https://www.devze.com 2023-02-20 16:44 出处:网络
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 in

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 li开发者_运维知识库ke (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] )


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


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


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);
}


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消