Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a weird problem, I am getting data straight from the database into a data source. The data is binded in a repeater with link buttons.

The problem I am having is that the data in the database might have multiple spaces in the middle of the string however when being displayed on the front end only space is being shown. After thinking about it this is standard HTML behavior to remove spaces, I would have assumed that asp.net would have handled this with the rendering of the webpage. What is also happening is that when reports are exported to excel it only has 1 space instead of two.

Example:

2 spaces: "South Africa   -   Cape Town"
single spaces: "South Africa - Cape Town"

This is an irrelevant example but my data has real use for multiple spaces.

share|improve this question
 
2 spaces: "South Africa[space][space]-[space][space]Cape Town"<br> single spaces: "South Africa[space]-[space]Cape Town" –  Kabir Bray Mar 29 '11 at 9:56
 
Is there a way to do this automatically on the code behind in c# so that we don't have to replace all strings being displayed? –  Kabir Bray Mar 29 '11 at 10:02
add comment

2 Answers

You could replace the spaces with &nbsp;, like this:

string result = myString.Replace(" ", "&nbsp;");

That would result in the HTML:

"South&nbsp;Africa&nbsp;&nbsp;-&nbsp;&nbsp;Cape&nbsp;Town"

Which would render correctly with two spaces.

share|improve this answer
 
Simple and efficient. +1 –  Hallaghan Mar 29 '11 at 9:57
 
Hi, thanks for your answer, appreciate it. I am looking for a way that will automatically do the replaces for me. Manually replacing each column throughout all the applications will be a lot of work. –  Kabir Bray Mar 29 '11 at 10:08
 
Well, depending on what you are working with here you could probably iterate through the control tree on some event, such as DataBound, and search and replace the spaces at that point. –  Mikael Östberg Mar 29 '11 at 10:15
 
One way would be to store it with the html code so you don't have to do so much work on reading the data. Would result in better perf but restricts you to only using the data in a web app. I don't know if that'd be acceptable to you. –  BenCr Mar 29 '11 at 10:16
add comment

Ended Up doing the following:

    Regex spaceRegex = new Regex("\\s{2,}");

    //Only replace subsequent spaces, not all spaces. For example, 
    //"   " (three spaces) becomes " &nbsp;&nbsp;" (1 space, 2 non-breaking spaces).
    MatchEvaluator matchEvaluator = delegate(Match match)
    {
        StringBuilder spaceStringBuilder = new StringBuilder(" ");
        for (int i = 0; i < match.Value.Length - 1; i++)
        {
            spaceStringBuilder.Append("&nbsp;");
        }
        return spaceStringBuilder.ToString();
    };

    return spaceRegex.Replace(server.HtmlEncode(value), matchEvaluator);
}
share|improve this answer
 
Pretty nice! One thing: Did you know that delegate shortcut has a new shortcut in the lambda statement since C# 3.0? You could use MatchEvaluator matchEvaluator = match => { ... }; –  Mikael Östberg Mar 30 '11 at 8:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.