| {\rtf1\ansi\ansicpg1252\deff0\deflang1044{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}} \viewkind4\uc1\pard\f0\fs17 Quickly remove extra whitespace from inside a string \par \par While VB doesn't offer a function that lets you remove extra whitespace inside a string, you can quickly whip up a custom function to do so on your own. In essence, using the Split() function, you can separate the string's component parts and then reconcatenate only those items in the resulting array that contain actual characters. \par \par As you know, the Split() function lets you split a string using specific characters as a delimiter. This function uses the following syntax: \par \par Split(string_to_split, delimiter) \par \par Split returns a variant array with each item representing the piece of the string between, but not including the delimiter character. So, for instance, the following function call \par \par Split("A, B, C, D", ",") \par \par would split the provided string into an array of four items--one for each letter, without the commas. \par \par Of course, you can also use a space (" ") as a delimiter. Naturally, a string might have one or more spaces in a row. Under these circumstances, Split() still splits the two spaces, but the item in the resulting array will contain an empty string. We can test for these empty strings in our custom function. The following code shows our custom Pack() function, and how it uses Split() to accomplish its goal. \par \par Private Sub Form_Load() \par MsgBox Pack("Hello there, world! What is going on?") \par \par End Sub \par Private Function Pack(str As String) As String \par Dim words As Variant \par Dim x As Long \par Dim temp As String \par \par words = Split(str, " ") \par For x = LBound(words) To UBound(words) \par If words(x) <> "" Then \par temp = temp & " " & words(x) \par End If \par Next x \par Pack = temp \par End Function \par } |
Quickly remove extra whitespace from inside a stri |
India web developer web development India | India web development company India ecommerce web developer