Page 1 of 1
String Handling Question
Posted: Wed Jun 01, 2011 7:55 pm
by jim wood
Guys,
I'm currently trying to extract a name from an email address. For example I have
example.a.example@examplecompany.com. I have easily been able to get to example.a.example but I'm toying with the best way of converting it to Example A Example. The problem I have is that some email addresses will have 2 dots and some will have 1 and some may not have a dot at all.
If any of you have hit something similar and could pass some pointers my way I would be grateful,
Jim.
Re: String Handling Question
Posted: Wed Jun 01, 2011 7:59 pm
by jim wood
I'm doing this in TI by the way, not rules. I'm looking in to using a while statement. If you hear an explosion from New York you know my toys have well and truely left the pram!!!
Re: String Handling Question
Posted: Wed Jun 01, 2011 8:58 pm
by Alan Kirk
jim wood wrote:
I'm currently trying to extract a name from an email address. For example I have
example.a.example@examplecompany.com. I have easily been able to get to example.a.example but I'm toying with the best way of converting it to Example A Example. The problem I have is that some email addresses will have 2 dots and some will have 1 and some may not have a dot at all.
I'm doing this in TI by the way, not rules. I'm looking in to using a while statement. If you hear an explosion from New York you know my toys have well and truely left the pram!!!
A While loop is the easiest approach since there's no equivalent of Replace in TI. It's also the most flexible as you can have an If block which checks for the period (
or any other character that you may want to check for in future) and replace it with a space. A combination of the Long function to get the length of the string, the While function and Subst function to pull out each character as you iterate through the string length (and also Code, if you want to check for unusual characters) and bang, you're done.
Re: String Handling Question
Posted: Thu Jun 02, 2011 6:48 am
by 7Zark7
You could try this:
myStrLen=SCAN('@',v1)-1;
myStr=SUBST(v1, 1, myStrLen);
myCharIndex = 1;
myFinalString='';
# test for '.' or ' ' then goto next char
While(myCharIndex<=myStrLen);
mystring=SUBST(myStr, myCharIndex, 1);
if(myString@='.' % mystring@= ' ');
myCharIndex = myCharIndex + 1;
mystring=SUBST(myStr, myCharIndex, 1);
myFinalString=myFinalString|' '|myString;
else;
mystring=SUBST(myStr, myCharIndex, 1);
myFinalString=myFinalString|myString;
endif;
myCharIndex = myCharIndex + 1;
end;
outputStr=myFinalString;
# capitalise strings
emailString=CAPIT(outputStr);
then you would need to concatenate the remainder of the string after emailString to get the result.
Cheers
Zark
Re: String Handling Question
Posted: Thu Jun 02, 2011 9:51 am
by jstrygner
You can also use Scan to find '.' (or ' ' or anything), just like you used it for finding '@'.
The loop will still be needed (you don't know the number of dots/spaces) but you do not need to iterate through whole string character by character (so it should work faster).
If Scan doesn't find the string it returns 0.
It would be nice to have a Replace/Substitute function, so you don't need to write a loop every time you need a substitution from one character to another.
Re: String Handling Question
Posted: Thu Jun 02, 2011 12:23 pm
by jim wood
Thanks for all the input guys and thanks Zark for posting up some code for me,
Jim.