Link to home
Start Free TrialLog in
Avatar of sharepoint0520
sharepoint0520

asked on

How to trim the string in javascript?

Experts,
 We have some java script and reading data from sharepoint list. Some list item has more space and we need to trim it before we compare. The  original value is "California          " and we need to replace to "California".

Can you please help me write some code?

original code
  var spClientContext = SP.ClientContext.get_current();
        var parentListName = 'DM Nomination List'; 
		var spList = spClientContext.get_web().get_lists().getByTitle(parentListName);
		var objResult;
		    
		    arrLstDMNomination = [];
		    
			var viewXml = "<View><Query><Where><Eq><FieldRef Name='Nominated' /><Value Type='Text'>1</Value></Eq></Where>";
				viewXml += "<GroupBy Collapse=\"TRUE\" ><FieldRef Name=\"Territory\"/></GroupBy></Query><ViewFields><FieldRef Name=\"LinkTitle\"/>";
				viewXml += "<FieldRef Name=\"Title\"/><FieldRef Name=\"Territory\"/></ViewFields><Aggregations Value=\"On\">";
				viewXml += "<FieldRef Name=\"Nominated\" Type=\"COUNT\"/></Aggregations></View>";
			 var groupBy = spList.renderListData(viewXml);
			 spClientContext.executeQueryAsync(function(){
			 objResults = JSON.parse(groupBy.m_value);
 			 
			  $.each(objResults.Row, function(idx, obj){
                var vTerritoryName = obj["Territory"];
			 	var vNominatedCnt = obj["Territory.COUNT.group"];
			 	arrLstDMNomination[idx] = { 'TerritoryName' : vTerritoryName, 'NominatedCount' : vNominatedCnt  };
              });	 
            //Step 4  
			//fnReadDMPCSelectedData();	          
 
			},function(e){
			 console.log(e);
			})

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

var str = "California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
str = str.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"");

Open in new window

User generated image
Avatar of sharepoint0520
sharepoint0520

ASKER

Thank you. It's object so where should i implement in attached code?
in your comparaison code, I don't see it...
Hi,
 I am using this code for comparision. I tried to add your code but did not work out.
$.each(arrLstDMNomination, function(idxC, objC){
			        	    objC.TerritoryName.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"");	
			        	    
			               if(objC.TerritoryName.replace("&amp;","&") == objA.TerritoryName){
			                  vDMNomCnt = objC.NominatedCount;
			                  vDMNomination = parseInt(vDMNomination) + parseInt(vDMNomCnt);
			                  }
			        	});

Open in new window

Hi, One more thing. There is extra space at end and that is why it's not working.

var str = "California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ";
str = str.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"");

And i have changed the code to compare

if(objC.TerritoryName.replace("&amp;","&") == objA.TerritoryName)
			                  {
			                  vDMNomCnt = objC.NominatedCount;
			                  vDMNomination = parseInt(vDMNomination) + parseInt(vDMNomCnt);
			                  }
			                  
			                  if(objC.TerritoryName.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"") == objA.TerritoryName)
			                  {
			                  vDMNomCnt = objC.NominatedCount;
			                  vDMNomination = parseInt(vDMNomination) + parseInt(vDMNomCnt);
			                  }

Open in new window

But not working.
so replace :
objC.TerritoryName.replace("&amp;","&")
by :
objC.TerritoryName.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"")
if doesn't work, try this too:
if(objC.TerritoryName.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"") == objA..replace(/^(&nbsp;)*|(&nbsp;)*$/g,""))
Thank you Sir.  There was a space after last" &nbsp;" so i had to replace " " to ""  again to accomplished.

var str = "California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ";
str = str.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"");

Thank you Sir.
remove also leading and ending space :
.replace(/^(&nbsp;)*|(&nbsp;)*$|^\s|\s$/g,"") 

Open in new window

Hi,
 Can you please check this on Browser?  It's not working. If i remove space from end then it's going to work out.

var str = "California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ";
str = str.replace(/^(&nbsp;)*|(&nbsp;)*$|^\s|\s$/g,"");

User generated image
Thank You
\s*
Instead
\s
Hi,
 Sorry to say but it did not work. Did you get chance to run on Browser?  I came up with alternate option but i have to replace twice.

var str = "California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ";
str = str.replace(/&nbsp;/gi,"").replace(" ","");

But this is not a ideal solution because if string has more  spaces at end then it will not work.
I'm on mobile phone, did you ads wildcard After each \s ?
\s*
I did it.  And this code also remove the space between two words too (that i did not expect). Please validate once you get chance.

Thank you so much for helping me.
var str = "California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ";
str = str.replace(/^(&nbsp;\s?)*|(&nbsp;\s?)*$/g,""); 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome. It's working like charm. Thanks a lot.