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 &nb sp; & nbsp;  ; &nb sp; " and we need to replace to "California".
Can you please help me write some code?
original code
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 &nb
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);
})
ASKER
Thank you. It's object so where should i implement in attached code?
in your comparaison code, I don't see it...
ASKER
Hi,
I am using this code for comparision. I tried to add your code but did not work out.
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(/^( )*|( )*$/g,"");
if(objC.TerritoryName.replace("&","&") == objA.TerritoryName){
vDMNomCnt = objC.NominatedCount;
vDMNomination = parseInt(vDMNomination) + parseInt(vDMNomCnt);
}
});
ASKER
Hi, One more thing. There is extra space at end and that is why it's not working.
var str = "California &nb sp; & nbsp;  ; &nb sp; ";
str = str.replace(/^( )*|(& nbsp;)*$/g ,"");
And i have changed the code to compare
var str = "California &nb
str = str.replace(/^( )*|(&
And i have changed the code to compare
if(objC.TerritoryName.replace("&","&") == objA.TerritoryName)
{
vDMNomCnt = objC.NominatedCount;
vDMNomination = parseInt(vDMNomination) + parseInt(vDMNomCnt);
}
if(objC.TerritoryName.replace(/^( )*|( )*$/g,"") == objA.TerritoryName)
{
vDMNomCnt = objC.NominatedCount;
vDMNomination = parseInt(vDMNomination) + parseInt(vDMNomCnt);
}
But not working.
so replace :
objC.TerritoryName.replace ("&"," &")
by :
objC.TerritoryName.replace (/^( )*|( )*$/g,"")
objC.TerritoryName.replace
by :
objC.TerritoryName.replace
if doesn't work, try this too:
if(objC.TerritoryName.repl ace(/^(&nb sp;)*|(&nb sp;)*$/g," ") == objA..replace(/^( )*| ( )*$ /g,""))
if(objC.TerritoryName.repl
ASKER
Thank you Sir. There was a space after last" " so i had to replace " " to "" again to accomplished.
var str = "California &nb sp; & nbsp;  ; &nb sp; ";
str = str.replace(/^( )*|(& nbsp;)*$/g ,"");
Thank you Sir.
var str = "California &nb
str = str.replace(/^( )*|(&
Thank you Sir.
remove also leading and ending space :
.replace(/^( )*|( )*$|^\s|\s$/g,"")
ASKER
\s*
Instead
\s
Instead
\s
ASKER
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 &nb sp; & nbsp;  ; &nb sp; ";
str = str.replace(/ /gi,"") .replace(" ","");
But this is not a ideal solution because if string has more spaces at end then it will not work.
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 &nb
str = str.replace(/ /gi,"")
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*
\s*
ASKER
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.
Thank you so much for helping me.
var str = "California ";
str = str.replace(/^( \s?)*|( \s?)*$/g,"");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome. It's working like charm. Thanks a lot.
Open in new window