I am using the code below to take a string entered from a text box, and convert to capital case, except for words like (the, and, an, as, to, or, on) etc.
Issue #1: I want the first word of the string to always be capitalized regardless of what the word is.
Issue #2: The word spacing is not correct when the string is put back together.
xText = queryForHTML
xTextSplit = split(xText, " ")
for each item in xTextSplit
xWord = item
if lcase(item) = "the" or lcase(item) = "and" or lcase(item) = "an" or lcase(item) = "as" or lcase(item) = "to" or lcase(item) = "is" or lcase(item) = "on" then
xWord = lcase(item)
end if
xCompleteWord = xCompleteWord & " " & xWord
next
queryForHTML = xCompleteWord
Option Explicit
Dim originalString
originalString = "a saMple of String capiTalization (in some cases, not so obvious)"
Dim convertedString
Dim noiseWords
noiseWords= "/a/abaft/aboard/about/above/absent/across/afore/after/against/along/alongside/amid" + _
"/amidst/among/amongst/an/anenst/apropos/apud/around/as/aside/astride/at/athwart/atop" + _
"/barring/before/behind/below/beneath/beside/besides/between/beyond/but/by/circa" + _
"/concerning/despite/down/during/except/excluding/failing/following/for/forenenst/from" + _
"/given/in/including/inside/into/like/mid/midst/minus/modulo/near/next/notwithstanding" + _
"/o/of/off/on/onto/opposite/or/out/outside/over/pace/past/per/plus/pro/qua/regarding" + _
"/round/sans/save/since/so/than/through/thru/throughout/thruout/till/times/to/toward" + _
"/towards/under/underneath/unlike/until/unto/up/upon/versus/vs/via/vice/vis/with/within" + _
"/without/worth/this/"
Function correctCase(matchString,word,position,sourceString)
word = LCase(word)
If (position > 0) And (InStr(noiseWords,"/" & word & "/")>0) Then
correctCase = word
Else
correctCase = UCase(Left(word,1)) & Mid(word,2,Len(word)-1)
End If
End Function
With New RegExp
.Pattern = "(\w+)"
.Global = True
.IgnoreCase = True
convertedString = .Replace(originalString,GetRef("correctCase"))
End With
WScript.Echo originalString
WScript.Echo convertedString
The basic idea is to use a regular expression matching any sequence of "word" characters ([a-zA-Z0-9]) and for each sequence, a function is called which receives as parameters the string matches, the capture group containing the word, the position in the string where it has been found and the full source string.
If the word is at position 0 it is capitalized. If the word is a "noise" word, it is lowercased, else, the word is capitalized.
The following code is based around the GetStringTypeW() Win32 API function, which provides information about characters in a string. You are only worried about characters which can be upper case or lower case. The problem with your code is that it only works with the simplest case where spaces break up words. But words can be broken up by punctuation. And there are many Unicode characters which have no concept of "upper case" and "lower case".
Instead of writing this boring, error prone code to do this, I take advantage of GetStringTypeW(). I iterate through each element in the array, where each element corresponds to a character in the string in the same position. I have a flag bInWord which stores whether the current position is inside a word. If we hit an upper or lower case character, and this hasn't been set, we set it, and save the current position as the start of the word. In addition, if we have hit an upper case character, and we already know we are in a word, then we there and then make the character lower case, by writing into the returned string.
When we hit non-alphabetical characters, or reach the end of the string, and bInWord is set, we then compare the last word with the list of "non-proper-cased" words. If we match, and the first character is upper case, then we overwrite the character with a lower case character. If we don't match, and the first character is lower-case, we overwrite the character with an upper case character.
Option Explicit
Private Declare Function GetStringTypeW Lib "Kernel32.dll" ( _
ByVal dwInfoType As Long, _
ByVal lpSrcStr As Long, _
ByVal cchSrc As Long, _
ByRef lpCharType As Integer _
) As Long
Private Const CT_CTYPE1 As Long = &H1
Private Const C1_UPPER As Long = &H1 ' Uppercase
Private Const C1_LOWER As Long = &H2 ' Lowercase
Private Const C1_DIGIT As Long = &H4 ' Decimal digits
Private Const C1_SPACE As Long = &H8 ' Space characters
Private Const C1_PUNCT As Long = &H10 ' Punctuation
Private Const C1_CNTRL As Long = &H20 ' Control characters
Private Const C1_BLANK As Long = &H40 ' Blank characters
Private Const C1_XDIGIT As Long = &H80 ' Hexadecimal digits
Private Const C1_ALPHA As Long = &H100 ' Any linguistic character: alphabetical, syllabary, or ideographic
Private Const C1_DEFINED As Long = &H200 ' A defined character, but not one of the other C1_* types
Private Function ProperCaseWords(ByRef in_sText As String) As String
Dim lTextLen As Long
Dim aiCharType() As Integer
Dim lPos As Long
Dim lPosStartWord As Long
Dim bInWord As Boolean
Dim bFirstCharUCase As Boolean
Dim sWord As String
' Output buffer contains a copy of the original string.
ProperCaseWords = in_sText
lTextLen = Len(in_sText)
' Resize the character type buffer to be one more than the string.
ReDim aiCharType(1 To lTextLen + 1)
' Retrieve string type data about this Unicode string into <aiCharType()>.
' If it fails, then we just return the original string.
' Note that the last element in the array is not filled by this function, and will contain zero.
' This is deliberate, so we can handle the corner case where the last word is right at the end of the string.
If (GetStringTypeW(CT_CTYPE1, StrPtr(ProperCaseWords), lTextLen, aiCharType(1))) = 0 Then
Exit Function
End If
' We start outside a word.
bInWord = False
' Iterate through the entire array, including the last element which corresponds to no character.
For lPos = 1 To lTextLen + 1
If (aiCharType(lPos) And C1_LOWER) = C1_LOWER Then
' Lower case characters.
If Not bInWord Then
bFirstCharUCase = False
lPosStartWord = lPos
bInWord = True
End If
ElseIf (aiCharType(lPos) And C1_UPPER) = C1_UPPER Then
' Upper case characters.
If bInWord Then
' If we are already in the word, i.e. past the first character, then we know that the character *should* be lower case.
Mid$(ProperCaseWords, lPos, 1) = LCase$(Mid$(ProperCaseWords, lPos, 1))
Else
bFirstCharUCase = True
lPosStartWord = lPos
bInWord = True
End If
Else
' Non lower or upper case characters. Also includes last (zero) element.
If bInWord Then
' If we are in a word, and the latest character is non-alphabetical, then we now check what word it is, and
' decide whether to make the first character upper or lower case.
bInWord = False
' Retrieve the word from the string, and deliberately make the first character lower case.
' Note that all other characters in the word would have already been made lower case.
sWord = Mid$(ProperCaseWords, lPosStartWord, lPos - lPosStartWord)
If bFirstCharUCase Then
Mid$(sWord, 1, 1) = LCase$(Mid$(sWord, 1, 1))
End If
' Compare our word against a lower-case word list.
Select Case sWord
Case "in", "on", "an", "to", "and", "the", "with", "that", "is" ' <=== CUSTOM LIST OF WORDS
If bFirstCharUCase Then
Mid$(ProperCaseWords, lPosStartWord, 1) = LCase$(Mid$(ProperCaseWords, lPosStartWord, 1))
End If
Case Else
If Not bFirstCharUCase Then
Mid$(ProperCaseWords, lPosStartWord, 1) = UCase$(Mid$(ProperCaseWords, lPosStartWord, 1))
End If
End Select
End If
End If
Next lPos
End Function
Related
Using this SO question / answer as a starting point: Splitting a single word into an array of the consituent letters
I have this simple bit of code to take a word and split the word into single letters:
<%
Dim word1, i
word1 = "particle"
For i = 1 To Len(word1)
Response.Write "<p>" & Mid(word1, i, 1) & "</p>"
Next
%>
I would like to know how to take a word (variable length, rather than a word that is 8 characters long as in the example above), and randomly rearrange the letters of the word - so that e.g. particle could be e.g.:
alpreict
lircpaet
ctelaipr
teapclir
raeitclp
This is an example of what I'd like to achieve: https://onlinerandomtools.com/shuffle-letters
However, I realise that is easier said than done.
I wondered if anyone has any advice about how it might be possible to achieve this using Classic ASP please?
Thanks
Here's one way to do it:
Function ShuffleText(p_sText)
Dim iLength
Dim iIndex
Dim iCounter
Dim sLetter
Dim sText
Dim sShuffledText
' Copy text passed as parameter
sText = p_sText
' Get text length
iLength = Len(sText)
For iCounter = iLength To 1 Step -1
' Get random index
iIndex = 1 + Int(Rnd * (iCounter))
' Get character at that index
sLetter = Mid(sText, iIndex, 1)
' Remove character from string
sText = Left(sText, iIndex - 1) & Mid(sText, iIndex + 1)
' Add character to shuffled string
sShuffledText = sShuffledText & sLetter
Next
' Return shuffled text
ShuffleText = sShuffledText
End Function
This code selects a random character in the string, removes it and adds it to a shuffled string. It repeats this process until it has gone through all characters.
There are probably more efficient ways to do this by randomizing an array of numbers first and using those numbers as iIndex, without manipulating the sText string.
hi all i have this question as bellow
how capitalize full in one vb6 Vb6 string variable
‘example
‘my fullname
Dim fullname as string
Fullname = “abdirahman abdirisaq ali”
Msgbox capitalize(fullname)
it prints abdirahmanAbdirisaq ali that means it skips the middle name space even if I add more spaces its same .
this is my own code and efforts it takes me at least 2 hours and still .
I tired it tired tired please save me thanks more.
Please check my code and help me what is type of mistakes I wrote .
This is my code
Private Function capitalize(txt As String) As String
txt = LTrim(txt)
temp_str = ""
Start_From = 1
spacing = 0
For i = 1 To Len(txt)
If i = 1 Then
temp_str = UCase(Left(txt, i))
Else
Start_From = Start_From + 1
If Mid(txt, i, 1) = " " Then
Start_From = i
spacing = spacing + 1
temp_str = temp_str & UCase(Mid(txt, Start_From + 1, 1))
Start_From = Start_From + 1
Else
temp_str = temp_str & LCase(Mid(txt, Start_From, 1))
End If
End If
Next i
checkName = temp_str
End Function
It's far simpler than that. In VB6 you should use Option Explicit to properly type your variables. That also requires you to declare them.
Option Explicit
Private Function capitalize(txt As String) As String
Dim temp_str as String
Dim Names As Variant
Dim Index As Long
'Remove leading and trailing spaces
temp_str = Trim$(txt)
'Remove any duplicate spaces just to be sure.
Do While Instr(temp_str, " ") > 0
temp_str = Replace(temp_str, " ", " ")
Loop
'Create an array of the individual names, separating them by the space delimiter
Names = Split(temp_str, " ")
'Now put them, back together with capitalisation
temp_str = vbnullstring
For Index = 0 to Ubound(Names)
temp_str = temp_str + Ucase$(Left$(Names(Index),1)) + Mid$(Names(Index),2) + " "
Next
'Remove trailing space
capitalize = Left$(temp_str, Len(temp_str) - 1)
End Function
That's the fairly easy part. If you are only going to handle people's names it still needs more work to handle names like MacFarland, O'Connor, etc.
Business names get more complicated with since they can have a name like "Village on the Lake Apartments" where some words are not capitalized. It's a legal business name so the capitalization is important.
Professional and business suffixes can also be problematic if everything is in lower case - like phd should be PhD, llc should be LLC, and iii, as in John Smith III, would come out Iii.
There is also a VB6 function that will capitalize the first letter of each word. It is StrConv(string,vbProperCase) but it also sets everything that is not the first letter to lower case. So PhD becomes Phd and III becomes Iii. Where as the above code does not change the trailing portion to lower case so if it is entered correctly it remains correct.
Try this
Option Explicit
Private Sub Form_Load()
MsgBox capitalize("abdirahman abdirisaq ali")
MsgBox capitalize("abdirahman abdirisaq ali")
End Sub
Private Function capitalize(txt As String) As String
Dim Names() As String
Dim NewNames() As String
Dim i As Integer
Dim j As Integer
Names = Split(txt, " ")
j = 0
For i = 0 To UBound(Names)
If Names(i) <> "" Then
Mid(Names(i), 1, 1) = UCase(Left(Names(i), 1))
ReDim Preserve NewNames(j)
NewNames(j) = Names(i)
j = j + 1
End If
Next
capitalize = Join(NewNames, " ")
End Function
Use the VB6 statement
Names = StrConv(Names, vbProperCase)
it's all you need (use your own variable instead of Names)
I have a string like X5BC8373XXX. Where X = a special character equals a Square.
I also have some special characters like \n but I remove them, but I can't remove the squares...
I'd like to know how to remove it.
I Found this method:
Dim Test As String
Test = Replace(Mscomm1.Input, Chr(160), Chr(64) 'Here I remove some of the special characters like \n
Test = Left$(Test, Len(Test) -2)
Test = Right$(Test, Len(Test) -2)
This method DOES remove those special characters, but it's also removing my first character 5.
I realize that this method just remove 2 characters from the left and the right,
but how could I work around this to remove these special characters ?
Also I saw something with vblF, CtrlF something like this, but I couldn't work with this ;\
You can use regular expressions. If you want to remove everything that's not a number or letter, you can use the code below. If there are other characters you want to keep, regular expressions are highly customizable, but can get a little confusing.
This also has the benefit of doing the whole string at once, instead of character by character.
You'll need to reference Microsoft VBScript Regular Expressions in your project.
Function AlphaNum(OldString As String)
Dim RE As New RegExp
RE.Pattern = "[^A-Za-z0-9]"
RE.Global = True
AlphaNum = RE.Replace(OldString, "")
End Function
Cleaning out non-printable characters is easy enough. One brute-force but easily customizable method might be:
Private Function Printable(ByVal Text As String) As String
Dim I As Long
Dim Char As String
Dim Count As Long
Printable = Text 'Allocate space, same width as original.
For I = 1 To Len(Text)
Char = Mid$(Text, I, 1)
If Char Like "[ -~]" Then
'Char was in the range " " through "~" so keep it.
Count = Count + 1
Mid$(Printable, Count, 1) = Char
End If
Next
Printable = Left$(Printable, Count)
End Function
Private Sub Test()
Dim S As String
S = vbVerticalTab & "ABC" & vbFormFeed & vbBack
Text1.Text = S 'Shows "boxes" or "?" depending on the font.
Text2.Text = Printable(S)
End Sub
This will remove control characters (below CHR(32))
Function CleanString(strBefore As String) As String
CleanString = ""
Dim strAfter As String
Dim intAscii As Integer
Dim strTest As String
Dim dblX As Double
Dim dblLen As Double
intLen = Len(strBefore)
For dblX = 1 To dblLen
strTest = Mid(strBefore, dblX, 1)
If Asc(strTest) < 32 Then
strTest = " "
End If
strAfter = strAfter & strTest
Next dblX
CleanString = strAfter
End Function
I have a space delimited string variable. I would like to store the contents of the variable into an array. Using split, I can store every space delimited value in an array. I would prefer if I could separate the string variable at every 7th space. For example, the text could read:
"hello hello hello hello hello hello hello hi hi hi hi hi hi hi hey hey hey hey hey hey hey"
This isn't the actual content of the string, but a simpler version that is easier to read. I want to separate at the places where the words change, or every 7th space. Any help would be greatly appreciated. My current code looks like this, which splits at every space.
ReDim StatsArray(ArrInc)
StatsArray = Split(Stats)
For i = 0 To UBound(StatsArray())
If i > UBound(StatsArray()) Then
ReDim Preserve StatsArray(i + ArrInc)
End If
' MsgBox StatsArray(i) ' When not commented out, this help me check the array
Next
There isn't any built-in function that will do this for you. A couple of solutions come to mind: (1) Do your split, then iterate your array. Concatenate seven array elements in a string variable. Write the result to a new array. Rinse and repeat. (2) Create an Array. Iterate through the string character by character, pushing each character into a variable and keeping track of the spaces you encounter; when you reach the seventh space add an element to your array, copy the variable to it, and clear the variable. Rinse and repeat.
The first one strikes me as a bit faster, but I could be quite wrong about that.
If I understand what you're trying to do, a string of "1234 12345678 123" would get split into 1234, 1234567, 8, 123. Is this correct?
If so, then you can use Regular Expressions to do the split for you.
Function Split7(S As String)
Dim regex As New RegExp
regex.Pattern = "[^ ]{7}"
regex.Global = True
Split7 = Split(regex.Replace(S, "$& "), " ")
End Function
This will insert a space after every 7th character that is not a space. Then use the split function to get the whole thing into an array.
Another stab at it, though I'd probably optimize the ReDim Preserves, doing them in chunks:
Option Explicit
Private Function SplitN( _
ByRef Source As String, _
ByVal Delim As String, _
ByVal N As Long) As String()
'Delim can be multi-character.
'Always returns at least 1-element result,
'even if Source = "".
Dim SearchPos As Long
Dim PartPos As Long
Dim DelimPos As Long
Dim Parts() As String
Dim DelimCount As Long
Dim PartsCount As Long
SearchPos = 1
PartPos = SearchPos
Do
DelimPos = InStr(SearchPos, Source, Delim)
If DelimPos > 0 Then
DelimCount = DelimCount + 1
If DelimCount = N Then
DelimCount = 0
ReDim Preserve Parts(PartsCount)
Parts(PartsCount) = _
Mid$(Source, PartPos, DelimPos - PartPos)
PartsCount = PartsCount + 1
PartPos = DelimPos + Len(Delim)
End If
SearchPos = DelimPos + Len(Delim)
End If
Loop While DelimPos > 0
ReDim Preserve Parts(PartsCount)
Parts(PartsCount) = Mid$(Source, PartPos)
SplitN = Parts
End Function
Private Sub Form_Load()
Dim S As String
Dim Parts() As String
Dim P As Long
S = "hello hello hello hello hello hello hello " _
& "hi hi hi hi hi hi hi " _
& "hey hey hey hey hey hey hey"
Text1.SelStart = 0
Text1.SelText = S & vbNewLine & vbNewLine
Parts = SplitN(S, " ", 7)
Text1.SelText = "Ubound() = " & UBound(Parts) & vbNewLine
For P = 0 To UBound(Parts)
Text1.SelText = Parts(P) & vbNewLine
Next
End Sub
I want to reduce the decimal length
text1.text = 2137.2198231578
From the above, i want to show only first 2 digit decimal number
Expected Output
text1.text = 2137.21
How to do this.
Format("2137.2198231578", "####.##")
I was about to post use Format() when I noticed p0rter comment.
Format(text1.text, "000.00")
I guess Int() will round down for you.
Been many years since I used VB6...
This function should do what you want (inline comments should explain what is happening):
Private Function FormatDecimals(ByVal Number As Double, ByVal DecimalPlaces As Integer) As String
Dim NumberString As String
Dim DecimalLocation As Integer
Dim i As Integer
Dim LeftHandSide As String
Dim RightHandSide As String
'convert the number to a string
NumberString = CStr(Number)
'find the decimal point
DecimalLocation = InStr(1, NumberString, ".")
'check to see if the decimal point was found
If DecimalLocation = 0 Then
'return the number if no decimal places required
If DecimalPlaces = 0 Then
FormatDecimals = NumberString
Exit Function
End If
'not a floating point number so add on the required number of zeros
NumberString = NumberString & "."
For i = 0 To DecimalPlaces
NumberString = NumberString & "0"
Next
FormatDecimals = NumberString
Exit Function
Else
'decimal point found
'split out the string based on the location of the decimal point
LeftHandSide = Mid(NumberString, 1, DecimalLocation - 1)
RightHandSide = Mid(NumberString, DecimalLocation + 1)
'if we don't want any decimal places just return the left hand side
If DecimalPlaces = 0 Then
FormatDecimals = LeftHandSide
Exit Function
End If
'make sure the right hand side if the required length
Do Until Len(RightHandSide) >= DecimalPlaces
RightHandSide = RightHandSide & "0"
Loop
'strip off any extra didgits that we dont want
RightHandSide = Left(RightHandSide, DecimalPlaces)
'return the new value
FormatDecimals = LeftHandSide & "." & RightHandSide
Exit Function
End If
End Function
Usage:
Debug.Print FormatDecimals(2137.2198231578, 2) 'outputs 2137.21
Looks fairly simple, but I must be missing something subtle here. What about:
Option Explicit
Private Function Fmt2Places(ByVal Value As Double) As String
Fmt2Places = Format$(Fix(Value * 100#) / 100#, "0.00")
End Function
Private Sub Form_Load()
Text1.Text = Fmt2Places(2137.2198231578)
End Sub
This also works in locales where the decimal point character is a comma.