Hello,
Dim MyString as string
MyString = “apples, banana, orange, pear,”
MyString = MyString.left(MyString.len-1)
This gives “apples, banana, orange, pear”
How can I replace that last comma with " and" ?
Thanks.
Lennox
Hello,
Dim MyString as string
MyString = “apples, banana, orange, pear,”
MyString = MyString.left(MyString.len-1)
This gives “apples, banana, orange, pear”
How can I replace that last comma with " and" ?
Thanks.
Lennox
Var myString As String = "apples, banana, orange, pear,"
// Trim the phrase, remove right comma, remove extra right spaces
// add the " and"
myString = myString.Trim.TrimRight(",").Trim
if myString>"" Then myString = myString + " and"
MessageBox myString
Quit
Thanks Rick,
A little clarification…
Now, I have “apples, banana, orange, pear”
So I want it to be “apples, banana, orange and pear”
Dim MyString As String
Dim MyArray() As String
MyString = “apples, banana, orange, pear,”
MyArray = MyString.Split(“,”)
MyString = “”
For xCount As Integer = 0 To MyArray.Count()-2
If xCount = MyArray.Count()-2 Then
MyString = MyString + " And " + MyArray(xCount) + “.”
Else
MyString = MyString + MyArray(xCount)+“,”
End If
Next
MessageBox(MyString)
Thanks Jose,
Exactly what I was trying to achieve.
Thanks again.
Lennox
Var myString As String = “apples, banana, orange, pear,”
myString=myString.TrimRight(“,”)
myString=myString.ReplaceAll(", ", “,”)
Var myStringElements() as string
myStringElements = myString.ToArray(“,”)
Var finalElement as string
finalElement=myStringElements.pop
if myStringElements.LastIndex=-1 then
return finalElement
else
return String.FromArray(myStringElements, ", ") + " and " + finalElement
end
Public Function NormalizeWordList(list As String) As String
// Normalize few simple cases and create a list
Var re As New RegEx
re.SearchPattern = "\s*,\s*"
re.ReplacementPattern = ","
re.Options.ReplaceAllMatches = True
// set the proper commas
list = re.Replace(list)
// remove orphan commas and split words
re.SearchPattern = ",,+"
Var words() As String = re.Replace(list).Trim.Trim(",").Split(",")
// No words
If words.Count = 0 Then Return ""
// One word
If words.Count = 1 Then Return words(0)
// Several words
Var lastWord As String = words(words.LastIndex)
words.RemoveAt(words.LastIndex)
Return String.FromArray(words, ", ")+" and "+lastWord
End Function
Good solutions.
Which is your favorite code?
![]()
Mine, of course. ![]()
But it really should be
“apples, banana, orange, and pear”
It’s called the “Oxford”, or “serial” comma, and I’m a fan.
And… Done.
Public Function NormalizeWordList(list As String, serialComma As Boolean = True) As String
// Normalize few simple cases and create a list
Var re As New RegEx
re.SearchPattern = "\s*,\s*"
re.ReplacementPattern = ","
re.Options.ReplaceAllMatches = True
// set the proper commas
list = re.Replace(list)
// remove orphan commas and split words
re.SearchPattern = ",,+"
Var words() As String = re.Replace(list).Trim.Trim(",").Split(",")
// No words
If words.Count = 0 Then Return ""
// One word
If words.Count = 1 Then Return words(0)
// Fix incorrect use of Oxford comma when less than 3 items
If words.Count = 2 Then serialComma = False
// Several words
Var lastWord As String = words(words.LastIndex)
words.RemoveAt(words.LastIndex)
Return String.FromArray(words, ", ") _
+ If(serialComma, ",", "") _
+ " and " _
+ lastWord
End Function
Came here to point this out. The world needs more Oxford commas.
My deleted reply was that meme about “the strippers, jfk and stalin” ![]()
I decided to remove it because it wasn’t a solution to the actual problem and I felt wrong for not helping. (I sometimes reply from bed on my mobile.)
FWIW, it wouldn’t be the same in other languages. In french, there’s no comma before the “and” word.
But it’s certainly not important in this context. I just learnt something.
Yeah, those southerners are a bit weird. In North Wales, where I’m from, we don’t do that. A comma suggests a pause in speaking, but you don’t pause while adding an and to the end of a list. Unless it’s something completely unexpected and you pause for effect: “apples, banana, orange, and oil well”
It’s a very old debate. When I say “Black, red, yellow, and green” there is indeed a slight pause after “yellow” - I don’t say “Black, red, yellowandgreen”.
It’s not really optional. Omitting one will change the meaning of your sentence. Now that this thread is solved…
If working with “lions,tigers,bears”, be sure to add the Oxford “oh my”.
Tim, that is such a good example.
I’m a great fan of regex, except I will never figure out how to create the proper patterns. Try Perplexity: tell it you’re coding in xojo and just describe what you want in plain english, provide the example like you did and the result you’re looking for. Even with complex string ops, it usually gets it right the first time. Ditto for complex UNIX Terminal commands. It is impressive.