Best way to remove the trailing comma (or whatever) from a string?
Best way to remove the trailing comma (or whatever) from a string?
(OP)
Curiosity getting the better of me... and knowing I won't be roasted by the current community... I hope. 
Assuming we have assembled a string like "1234,123,198,678," what are your favourite ways to remove the trailing comma?
Is ok if you are confident there is a trailing comma... Mike recently hinted that PadR() might be useful.

Assuming we have assembled a string like "1234,123,198,678," what are your favourite ways to remove the trailing comma?
CODE
m.STRING = LEFT(m.STRING,LEN(m.STRING)-1)
Is ok if you are confident there is a trailing comma... Mike recently hinted that PadR() might be useful.
Regards
Griff
Keep ing
There are 10 kinds of people in the world, those who understand binary and those who don't.
I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
There is no place like G28 X0 Y0 Z0
I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
There is no place like G28 X0 Y0 Z0
RE: Best way to remove the trailing comma (or whatever) from a string?
Did I? If so, that was clearly a mistake. The function is RTRIM():
lcString = RTRIM(lcString, ",")
And no harm is done if it happens that there is no trailing comma.
(I have now corrected thread184-1819024: bulk sms in vfp9.)
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
RE: Best way to remove the trailing comma (or whatever) from a string?
Regards
ing
Griff
Keep
I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
There is no place like G28 X0 Y0 Z0
RE: Best way to remove the trailing comma (or whatever) from a string?
RTRIM() is the way to go
you may even specify all the characters you want to be removed from the right edge
lcStr = "2345,6667,88.,bab"
? RTRIM(lcStr, ".",",","a","b")
hth
MarK
RE: Best way to remove the trailing comma (or whatever) from a string?
That option was only introduced in VFP 9.0. Before that, you could only use the function to remove trailing spaces.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
RE: Best way to remove the trailing comma (or whatever) from a string?
Suppose you have one or more comma inside a string, perhaps like this:
HELLO,,,WORLD,FROM,,,,TEK-TIPS
You want to eliminate all those commas, replacing each substring of commas with a single space, like so:
HELLO WORLD FROM TEK-TIPS
How would you go about that with the minimum amount of code?
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
RE: Best way to remove the trailing comma (or whatever) from a string?
Steve
RE: Best way to remove the trailing comma (or whatever) from a string?
That's right. The reason is historical. Back in dBASE days, they never thought we would ever want to trim the left-most spaces. So we had just plain TRIM(), which trimmed the right-most spaces. When LTRIM() was later introduced, they added RTRIM() to balance things out, as it were. But of course they kept the old TRIM(), so as not to break compatibility.
Personally, I always use RTRIM() in preference to TRIM() for the simple reason that it is more explicit. I expect most other developers do too.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
RE: Best way to remove the trailing comma (or whatever) from a string?
1. Build the string with the necessary commas only
2. Use comma prefixed before each item and finally use SUBSTR from the second position.
3. Add items with ccomma suffix and finally remove the surplus comma with LEFT() or RTRI/TRIM.
Assume there is a dbf or curssro alias items with an item field that's varchar and so has trimmed items (no trailing spaces as you have with a normal char field).
1. with IIF
CODE
2. prefix and SUBSTR
CODE
3. suffix and TRIM
CODE
And all of them have their negative point.
1. An IIF for every item
2. and 3: A final string operation.
But it's also pointless to rant about this necessary last operation in case 2 and 3, as every item you append with the assignment lcCSV = lcCSV + does nneed to allocate enough memeory for the overall string, and copies over lcCSV plus the item into it. VFP has no thing like C#'s stringbuilder class. And though VFP is programmed in C++ it also has no pointer to do tricks with, at least you don't have hands on it within VFP itself. There also isn't a function like PHPs implode().
There are surely more ways to do it
4. like 1, but without IIF:
CODE
5. like 1 but through special handling of first element
CODE
Now you can play with these and special cases like extremely many items or no items at all, etc. etc.
But you can't tell me that you can't think of any way on your own, at least to create the string with a surplus comma at the start or end. And then it's really just a short visitation of the help to look up how to get substrings, which functions exist for that, like LEFT, RIGHT, SUBSTR, or how you can use TRIM/RTRIM/LTRIM/ALLTRIM. Also ALLTRIM allows specifying other characters to trim away than whitespace.
It does really not play any mentionable role to do things the one or other way. If you have extremely many elements it would turn out to be better to create partial lists and concatenate them as a last step. If you have 1 million elements you would make 999.999 copies in average half the string length, and that would hurt the performance, but it would be a problem no matter which of the three major methods you choose. And the last step to remove the comma will never be a major part of the operation.
Chriss
RE: Best way to remove the trailing comma (or whatever) from a string?
CODE
Regards
ing
Griff
Keep
I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
There is no place like G28 X0 Y0 Z0
RE: Best way to remove the trailing comma (or whatever) from a string?
about your task to fix a string like "HELLO,,,WORLD,FROM,,,,TEK-TIPS".
I guess you think about the foxtools.fll, which has that function for it:
CODE
It's replaceing any number of commas with one space, though, so at the end you get HELLO WORLD FROM TEK-TIPS.
Now it would depend on whether single items (i.e two words or more, like full names) can contain spaces. If not, you could now replace every space with a comma:
CODE
Well, if you have spaces within items, what would work is first replacing them with something else to protect them as is.
CODE
Chriss
RE: Best way to remove the trailing comma (or whatever) from a string?
CODE --> Foxpro
RE: Best way to remove the trailing comma (or whatever) from a string?
CODE --> VFP
RE: Best way to remove the trailing comma (or whatever) from a string?
I'm guilty of the same misunderstanding. Aa the main topic was comma separated lists.
Indeed Reduce() of foxtools does that and wouldn't need a while loop. It would help to have a Reduce function in which you could also specify what character you want to end up with, too. So you could decide to get a string with single spaces or a comma separated list or whatever you want.
Chriss
RE: Best way to remove the trailing comma (or whatever) from a string?
CODE -->
I came looking for another answer and got pulled into Mike's exercise, DOH!
msc
RE: Best way to remove the trailing comma (or whatever) from a string?
CODE -->
SET LIBRARY TO "foxtools.fll" ? REDUCE("HELLO,,,WORLD,FROM,,,,TEK-TIPS", ",")
EDIT: Sorry, I missed out the second parameter to REDUCE(). I have now added it - see highlight).
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
RE: Best way to remove the trailing comma (or whatever) from a string?
you forgot to ask REDUCE to reduce one or multiple commas to spaces. The REDUCE as you posted it (REDUCE("HELLO,,,WORLD,FROM,,,,TEK-TIPS")) does not remove anything.
I see you corrected your post.
Using REDDUCE for other characters also has its downside, as already said:
CODE
If you'd like to get "Snails" as a result you'd need to replace the first space with "S" and the last one with "s".
More generally said, if you use REDUCE to reduce other characters than whitespace characters and reduce multiple characters, it becomes harder to reduce them to single instances of themselves, as REDUCE always reduces what it reduces to single spaces. So its main intent is to remove surplus whitespace, as could be found in HTML, for example, or in source code. Though in case of source code reduce should make a difference between spaces in string delimiters vs the rest of the code, where any number of spaces have the same meaning as a single space.
Chriss.
RE: Best way to remove the trailing comma (or whatever) from a string?
thanks!
RE: Best way to remove the trailing comma (or whatever) from a string?
Chriss
RE: Best way to remove the trailing comma (or whatever) from a string?
Of course, you can remove any character in this way, not just commas. You can also specify multiple characters in the second parameter, in which case they are treated independently, not as a single multi-character string.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
RE: Best way to remove the trailing comma (or whatever) from a string?
In fact, the only other function I have used recently is RGBComp(). Most of the other useful ones are now native to VFP.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
RE: Best way to remove the trailing comma (or whatever) from a string?
One SET LIBRARY and you have a slightly extended VFP language.
You won't get intellisense help when writing functions available through foxtools. That makes it a bit less pleasant than native functions. The necessary info for intellisense could be added to foxcode.dbf.
And now, if you're thinking this can be used to extend VFP language. Well, yes, but with functions only, an FLL can't define/declare new VFP commands, that's not foreseen in the structure of an FLL, especially the FoxInfo struture, that's described in the help, too: https://www.vfphelp.com/vfp9/_5wn12p9el.htm
This FoxInfo structre only can list functions and their parameterization.
Chriss
RE: Best way to remove the trailing comma (or whatever) from a string?
CODE
If you might not know or care if your list string has a leading or a trailing commma or both, here's how ALLTRIM trims off commas from both ends and keeps the middle ones:
CODE
All three variants result in: 1234,123,198,678. Well, and of course if the list already is correct, such an ALLTRIM does nothing, just like ALLTRIM on a string with neither leading nor trailing whitespace does anything.
Chriss
RE: Best way to remove the trailing comma (or whatever) from a string?
CODE
The obvious solution to get n spaces in a string is to use SPACE(n) instead of a while loop, but that's not the point. The point is to understand why adding just a single space to a string takes longer and longer, the longer the string is.
And I wouldn't have expected how high the performance degrades. It all plays a neglectible role from strings in the range of a few KB, but keep that in mind if you need to concatenate a lot of items.
Chriss
RE: Best way to remove the trailing comma (or whatever) from a string?
CODE --> VFP
(also, making sure there won't be duplicate spaces inside the string).
RE: Best way to remove the trailing comma (or whatever) from a string?
Best Regards,
Scott
MSc ISM, MIET, MASHRAE, CDCAP, CDCP, CDCS, CDCE, CTDC, CTIA, ATS, ATD
"I try to be nice, but sometimes my mouth doesn't cooperate."
RE: Best way to remove the trailing comma (or whatever) from a string?
All the 4 - TRIM, ALLTRIM, LTRIM and RTRIM accept more than 1 parameter - at least in VFP9. In addition you can specify them to be case-sensitive. Please have a look at the help file
hth
MarK
RE: Best way to remove the trailing comma (or whatever) from a string?
FWIW my favorite and most used way is to avoid placing the comma at the end of the string in the first place.
In general, I start building the string with the first item (without a comma) followed by the rest of the items in the string, each PRECEDED by a comma.
The "rest" of the items might be from looping through an array (e.g. field names) or whatever.
Steve
RE: Best way to remove the trailing comma (or whatever) from a string?
It rarely makes sense to create a list, if there are no items, but whenever an empty list also can make sense (as optional parameter, for example), you should also cover that. For that corner case you need special caution about knowing you have at least 1 element, a normal SCAN or FOR EACH loop covers that case without special handling, and you end with what you started: An empty string. Because if you start a SCAN loop on EOF() you actually don't ever enter the loop body and execute it, the loop ends before it begins, likewise a FOR loop with a final value lower than the initial also does end before it begins.
Chriss
RE: Best way to remove the trailing comma (or whatever) from a string?
Don't feel bad about that, Scott. The second parameter was only added in VFP 9.0. Before that, the only thing you could trim was spaces.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
RE: Best way to remove the trailing comma (or whatever) from a string?
Sorry, Chris. Didn't read close enough.
Steve
RE: Best way to remove the trailing comma (or whatever) from a string?
Well THAT explains it! You know, when you spend 25+ years with a function, and you "know how it works", so easy to overlook the addition of some new capability in it. I mean, how often do you read the Help file for a function you "already know"? :)
That said, I don't know when I would need it. ><
Best Regards,
Scott
MSc ISM, MIET, MASHRAE, CDCAP, CDCP, CDCS, CDCE, CTDC, CTIA, ATS, ATD
"I try to be nice, but sometimes my mouth doesn't cooperate."
RE: Best way to remove the trailing comma (or whatever) from a string?
Tamar
RE: Best way to remove the trailing comma (or whatever) from a string?
Do you realize I posted ALLTRIM with that additional parameter can be used as a universal solution to remove a surplus comma, no matter if it's a prefix or suffix comma. See my post from 29 Nov 22 21:10.
So actually it's usable as the answer to the main question of Griff. If that's of no use to you, then you may not ever create a comma separated items list or do it without surplus commas. Whatever, the way this additional parameter works is not only to change from whitespace to any other character, you can trim off any group of characters, and that can be useful for many kinds of operations, it's not always just whitespace that's unwanted.
Chriss
RE: Best way to remove the trailing comma (or whatever) from a string?
Yeah, and I read them when they came out, but I have big "gap years" between when I use VFP, and when I'm dormant. I went into developer retirement for 15 years, came back out, and picked VFP back up again around 2014. Since then I'm in and out. But in fairness, as I mentioned, the way I develop otherwise, I don't think I'd really ever use this extra parameter.
I do also watchin the auto help, where it tells you want parameters are used within the function as you type them, and I still never noticed this. I usually only use ALLTRIM anyway, anything else would have a test for length, and I'd likely use SUBSTR to get rid of unwanted characters.
Best Regards,
Scott
MSc ISM, MIET, MASHRAE, CDCAP, CDCP, CDCS, CDCE, CTDC, CTIA, ATS, ATD
"I try to be nice, but sometimes my mouth doesn't cooperate."
RE: Best way to remove the trailing comma (or whatever) from a string?
CODE -->
Which replaces all commas with a space, yielding:
HELLO WORLD FROM TEK-TIPS (the extra spaces are being removed by the HTML display...)
If the problem is to remove the extra spaces as well, thus leaving a single space separating each word, then VFP does not have a native function to perform that task. The use of the REDUCE function in the FoxTools FLL would be the answer in addition to the CHRTRAN function.
CODE -->
Greg
RE: Best way to remove the trailing comma (or whatever) from a string?
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads