Link to home
Start Free TrialLog in
Avatar of TheRookie32
TheRookie32

asked on

Changing @Function String Manipulations to Javascript

I have been tasked with coming up with a library of Javascript string manipulations that do the same function as their @formula language relatives...

@left, @right etc...

Any ideas on how to port them over to Javascript?  Example of @left?
Avatar of CRAK
CRAK
Flag of Netherlands image

Ref. e.g. http://www.dannyg.com/dl/JSB5RefBooklet.pdf
At page 8 you'll find "Text" and its methods and properties.

@Left(Txt, Nr) could easily be translated using Txt.substr(start, length)
@Left(Txt, char) would first require calculation of Txt.indexOf(char). Its result could then be fed into substr.
@Explode(Txt, char) would be Txt.split(char), so @Left(Txt,char) could also be written as Txt.split(char)(0).

Does that help?
Avatar of TheRookie32
TheRookie32

ASKER

Kinda, i am quite new to Javascript and writing full on functions so can you post an example using it?  The idea is to have the @Left function be transferred over to a Javascript function.  Meaning it is to accept a stringToSearch, numberOfCharacters and a subString.  Not be broken into two different ones...
Here is what i have so far, not working quite right yet:

function atLeft(stringToSearch,subStringOrNumChars)
{ var pos = -1;
  var len = stringToSearch.Length;
  if ( isNaN(subStringOrNumChars) )
  { pos = stringToSearch.indexOf(subStringOrNumChars);
    if ( pos > -1 )
    { return stringToSearch.substring(0,pos)
    }
    else
    {
    }
  }
  else
  {
  }
}
ASKER CERTIFIED SOLUTION
Avatar of CRAK
CRAK
Flag of Netherlands 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
Thanks for the points!
Interesting challenge you have there, for someone new to javascript! But it'll sure be educational! From what you already had I'm sure you'll do just fine!
Good luck! CU around!
Yes it has been interesting... I just did an atRight one which was a bit harder... but i got it.  And your welcome for the points. :)