SharePoint - String.Trim wont work :S - Asked By Jacob Söderblom on 28-Jun-12 02:58 AM

I can´t get rid of the whitespaces in a string.. Here is my code

if (!string.IsNullOrEmpty(b))
               {
                   Encoding encode = Encoding.Unicode;


                   byte[] bytes = Convert.FromBase64String(b);


                   MemoryStream memoryStream = new MemoryStream(bytes);


                   BinaryReader binary = new BinaryReader(memoryStream);


                   int fileSize = (int) binary.ReadUInt32();


                   int attachmentNameLength = (int) binary.ReadUInt32()*2;


                   byte[] fileNameBytes = binary.ReadBytes(attachmentNameLength);


                   string name = encode.GetString(fileNameBytes, 0, attachmentNameLength - 2);


                   byte[] decodeFile = binary.ReadBytes(fileSize);


                   name = ConvertSpecialCharacters(name);


                   item.Attachments.Add(name, decodeFile);


                   item.UpdateOverwriteVersion();
               }

private string ConvertSpecialCharacters(string stringToConvert)
       {
           stringToConvert = stringToConvert.Replace("~", string.Empty);
           stringToConvert = stringToConvert.Replace("#", string.Empty);
           stringToConvert = stringToConvert.Replace("%", string.Empty);
           stringToConvert = stringToConvert.Replace("&", string.Empty);
           stringToConvert = stringToConvert.Replace("*", string.Empty);
           stringToConvert = stringToConvert.Replace("{", string.Empty);
           stringToConvert = stringToConvert.Replace("}", string.Empty);
           stringToConvert = stringToConvert.Replace("\\", string.Empty);
           stringToConvert = stringToConvert.Replace(":", string.Empty);
           stringToConvert = stringToConvert.Replace("<", string.Empty);
           stringToConvert = stringToConvert.Replace(">", string.Empty);
           stringToConvert = stringToConvert.Replace("?", string.Empty);
           stringToConvert = stringToConvert.Replace("/", string.Empty);
           stringToConvert = stringToConvert.Replace("|", string.Empty);
           stringToConvert = stringToConvert.Replace("\"", string.Empty);
   stringToConvert = stringToConvert.Trim();

           return stringToConvert;
       }

Please help!
Neha Garg replied to Jacob Söderblom on 28-Jun-12 03:03 AM

Hello Jacob,

 

Use the below code:

 

Code:


String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
}

 
Usage:


var str = '   abc   '; // OR var str = new String('   abc   ');
var trimmedStr = str.trim();

 

 

See the examples and syntax given on the below links:

 

http://www.mindfiresolutions.com/How-to-Trim-a-String-Using-JavaScript-47.php

 

http://www.sharemuch.com/2010/07/04/trimming-sharepoint-2010-search-results-%E2%80%93-part-1/

 

Jacob Söderblom replied to Neha Garg on 28-Jun-12 03:23 AM
I have tried it.. it doesn´t work either..

Getting wierd names when i upload a documnet like .txt or .doc..
[)ia6l0 iii replied to Jacob Söderblom on 01-Jul-12 07:53 AM
The trim function will only get rid of trailing and leading spaces, not the spaces within a string. For that, you will have to manually replace all occurences of whitespace in the string. Note that there might be additional newline characters too, because you did mention of a document. 

At the end of your special character replacing lines, place this:
stringToConvert = stringToConvert.Replace(" ", string.Empty);

Hope that makes sense.