Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the below code used in web services.

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;

    namespace StringTest
    {
       /// <summary>
       /// Summary description for StringTest
       /// </summary>
       [WebService(Namespace = "http://tempuri.org/")]
       [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
       [System.ComponentModel.ToolboxItem(false)]
       // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
       // [System.Web.Script.Services.ScriptService]
       public class StringTest : System.Web.Services.WebService
       {

           [WebMethod]

           public string HelloWorld(string str)
           {
               return str;
           }
       }
    }





When I provide the input as "Hello         world", I'm getting the result as Hello world (Nine spaces between the words has been reduced to one).



How can i Prevent trimming spaces between words in webservices?


What I have tried:

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;

    namespace StringTest
    {
       /// <summary>
       /// Summary description for StringTest
       /// </summary>
       [WebService(Namespace = "http://tempuri.org/")]
       [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
       [System.ComponentModel.ToolboxItem(false)]
       // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
       // [System.Web.Script.Services.ScriptService]
       public class StringTest : System.Web.Services.WebService
       {

           [WebMethod]

           public string HelloWorld(string str)
           {
               return str;
           }
       }
    }
Posted
Updated 23-Jan-18 21:40pm
Comments
F-ES Sitecore 24-Jan-18 4:59am    
As OriginalGriff suggested, you can replace spaces with non-breaking ones but you should make sure only double spaces are replaced, if you replace all spaces with   then the text won't ever wrap in a container, it will be stuck on a single line. Also it might be tempting to do this replace in your HelloWorld web method but that method should always return the raw text and if any conversion on that text needs to be done to display it then that work should happen on the presentation layer, the web method shouldn't do it.

1 solution

That's a "feature" of HTML: spaces are always trimmed.
However, you can replace the spaces with "non blank space" characters: &nbsp;
Hello&nbsp;&nbsp;&nbsp;&nbsp;World
Becomes
Hello    World
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900