Breaking up strings
-
Hi, If I have text like: VariantDisplayMode How do I break it up so that it displays as: Variant Display Mode? Basically it is a seperation at the capital letters. Regards ma se
-
Hi, If I have text like: VariantDisplayMode How do I break it up so that it displays as: Variant Display Mode? Basically it is a seperation at the capital letters. Regards ma se
-
Hi, If I have text like: VariantDisplayMode How do I break it up so that it displays as: Variant Display Mode? Basically it is a seperation at the capital letters. Regards ma se
Another way would be to : -Create a character Array of your text using
ToCharArray()
-Test each character , if Caps add space before the character and add it to the resulting string otherwise add the character to the resulting string:Private Function TransFormText(ByVal oText As String) As String Dim oResult As String = "" Dim oArray() As Char = oText.ToCharArray() For Each oItem As Char In oArray If oItem. = oItem Then oResult = oResult & " " & oItem Else oResult = oResult & oItem End If Next Return Trim(oResult) End Function
-
Hi, If I have text like: VariantDisplayMode How do I break it up so that it displays as: Variant Display Mode? Basically it is a seperation at the capital letters. Regards ma se
There was an error in my previous post:
Private Function TransFormText(ByVal oText As String) As String Dim oResult As String = "" Dim oArray() As Char = oText.ToCharArray() For Each oItem As Char In oArray If Char.ToUpper(oItem) = oItem Then oResult = oResult & " " & oItem Else oResult = oResult & oItem End If Next Return Trim(oResult) End Function
-
Another way would be to : -Create a character Array of your text using
ToCharArray()
-Test each character , if Caps add space before the character and add it to the resulting string otherwise add the character to the resulting string:Private Function TransFormText(ByVal oText As String) As String Dim oResult As String = "" Dim oArray() As Char = oText.ToCharArray() For Each oItem As Char In oArray If oItem. = oItem Then oResult = oResult & " " & oItem Else oResult = oResult & oItem End If Next Return Trim(oResult) End Function
You should however mention that the method scales very badly, so it's only suitable for very short strings. For a string with 20 characters, it creates 40 string objects to produce the result. Also, for every additional character the memory usage doubles.
--- single minded; short sighted; long gone;
-
You should however mention that the method scales very badly, so it's only suitable for very short strings. For a string with 20 characters, it creates 40 string objects to produce the result. Also, for every additional character the memory usage doubles.
--- single minded; short sighted; long gone;
Guffa wrote:
You should however mention that the method scales very badly, so it's only suitable for very short strings. For a string with 20 characters, it creates 40 string objects to produce the result. Also, for every additional character the memory usage doubles.
sure, But its a good starter.Obviously the poster is a beginner and in such cases its better to provide a solution that is easy to understand and that build up logic...
-
Use the Regex class with a regular expression pattern like "([a-z])([A-Z])" to find the changes from lower case to upper case. Put a space between them by replacing the occurances using the pattern "$1 $2".
--- single minded; short sighted; long gone;
Guffa, I think that regular expression would fail to properly parse something like "ThisIsATest" because it wouldn't pick up on two capital letters next to one another. I think something like this would be required to find the word boundaries "([A-Z]|[a-z])(?=[A-Z])". Using a replace of "$1 ".
-
Guffa, I think that regular expression would fail to properly parse something like "ThisIsATest" because it wouldn't pick up on two capital letters next to one another. I think something like this would be required to find the word boundaries "([A-Z]|[a-z])(?=[A-Z])". Using a replace of "$1 ".
-
Guffa wrote:
You should however mention that the method scales very badly, so it's only suitable for very short strings. For a string with 20 characters, it creates 40 string objects to produce the result. Also, for every additional character the memory usage doubles.
sure, But its a good starter.Obviously the poster is a beginner and in such cases its better to provide a solution that is easy to understand and that build up logic...