?
Solved

Changing @Function String Manipulations to Javascript

Posted on 2006-05-18
6
Medium Priority
?
270 Views
Last Modified: 2013-12-18
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?
0
Comment
Question by:TheRookie32
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:CRAK
ID: 16711373
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?
0
 
LVL 6

Author Comment

by:TheRookie32
ID: 16711419
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...
0
 
LVL 6

Author Comment

by:TheRookie32
ID: 16712587
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
  {
  }
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 13

Accepted Solution

by:
CRAK earned 1000 total points
ID: 16715863
<html>
<head>
<script language="javascript">
function atLeft(stringToSearch,subStringOrNumChars)
{
  var pos = -1;
  var len = stringToSearch.length; // lowercase 'L'!
  if ( isNaN(subStringOrNumChars) )
  { //subStringOrNumChars appears string
    pos = stringToSearch.indexOf(subStringOrNumChars)
  }
  else
  { //subStringOrNumChars appears numeric
    pos = subStringOrNumChars
  }
  //regardless of subStringOrNumChars, pos now holds the cut-off position
  if ( pos > -1 )
  { return stringToSearch.substring(0,pos)
  }
  else
  { return '';
  }
}
</script>
</head>
<body>
<script language="javascript">
var str = 'abcde';
var br = '<br>';
document.writeln('<u>The test:</u><br>');
document.writeln('test string = ' + str + br);
document.writeln(' 2: ' + atLeft(str,  2 ) + br);
document.writeln(' 1: ' + atLeft(str,  1 ) + br);
document.writeln(' 0: ' + atLeft(str,  0 ) + br);
document.writeln('-2: ' + atLeft(str, -2 ) + br);
document.writeln('10: ' + atLeft(str, 10 ) + br);
document.writeln(' c: ' + atLeft(str, 'c') + br);
document.writeln(' d: ' + atLeft(str, 'd') + br);
document.writeln(' X: ' + atLeft(str, 'X') + br);
document.writeln('  : ' + atLeft(str,  '') + br);
</script>
</body>
</html>
0
 
LVL 13

Expert Comment

by:CRAK
ID: 16720981
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!
0
 
LVL 6

Author Comment

by:TheRookie32
ID: 16721145
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. :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginners of Lotus Notes user this is important to know about the types of files and their location supported by IBM Notes. Mostly users are unaware about how many file types are created and what their usages are. This Article is fully dedicated…
Lack of Storage capacity is a common problem that exists in every field of life. Here we are taking the case of Lotus Notes Emails, as we all know that we are totally depend on e-communication i.e. Emails. This article is fully dedicated to resolvin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question