[HELP] String Manipulation
hi all
i hve a little problem..
i wanna make simple function to generate/manipulate a string.
Example :
Input string :
vbforums
Output string must be :
v.bforums
vb.forums
vbf.orums
vbfo.rums
vbfor.ums
vbforu.ms
vbforum.s
v.b.forums
v.bf.orums
v.bfor.ums
..etc as posible string(no double)
look simple but a confused with this :mad:
please someone help me:wave:
Re: [HELP] String Manipulation
well, down to the double period, here's the solution...I'll work on the remainder in a moment.
Private Sub Command1_Click()
dim myText as string, myNewText as string, x as integer
myText = Text1.Text
For X = 1 To Len(Text1.Text)
myNewText = myNewText + Mid(myText, 1, X) + "." + Mid(myText, X + 1) + vbCr
Next X
End Sub
Re: [HELP] String Manipulation
Quote:
Originally Posted by
SamOscarBrown
well, down to the double period, here's the solution...I'll work on the remainder in a moment.
Private Sub Command1_Click()
dim myText as string, myNewText as string, x as integer
myText = Text1.Text
For X = 1 To Len(Text1.Text)
myNewText = myNewText + Mid(myText, 1, X) + "." + Mid(myText, X + 1) + vbCr
Next X
End Sub
thanks fo ur reply.
thats work but i need more output
whit your function output text just :
v.bforums
vb.forums
vbf.orums
vbfo.rums
vbfor.ums
vbforu.ms
vbforum.s
i mean all posible .(dot) watever 1 or 2 or many dot whit word : vbforums
like this:
v.bforums
vb.forums
vbf.orums
vbfo.rums
vbfor.ums
vbforu.ms
vbforum.s
v.b.forums
v.bf.orums
v.bfor.ums
v.bforu.ms
v.bforum.s
v.b.f.orums
.
.
.
vbforu.m.s
.
.
.
until
v.b.f.o.r.u.m.s
and i wanna add output text to Listview.
any idea?
please someone help me :(
Sorry for my grammar look bad..
Re: [HELP] String Manipulation
I suggest you think about this problem a little differently. Given any word of n letters, you have (n-1) slots where a period could appear. Think of those slots like binary number with (n-1) digits. So in the case of "vbforums", where n = 8, the binary number with which you would map the periods would consist of seven digits. Thus:
0000000 = "vbforums"
1000000 = "v.bforums"
...
0100010 = "vb.foru.ms"
...
1111111 = "v.b.f.o.r.u.m.s"
Thus, you have 2^(n-1) permutations of letters and periods. To generate your list, simply iterate from 1 to 2^(n-1) and generate the string accordingly. After that, if you want to place the string in some sort of order, sort it according to whatever rule you want (you could also re-code the mapping so that it generates the results in the correct order if you like, but that would probably be more difficult, although it would run faster).
So that's how to solve the problem. Now it's your job to actually solve it.
Re: [HELP] String Manipulation
Based on Lenggries' idea, this should work.
Code:
Option Explicit
Private Sub Command1_Click()
Dim i As Integer
Dim ub As Integer
Dim intMaskLen As Integer
intMaskLen = Len(Text1.Text) - 1
ub = 2 ^ intMaskLen
For i = 1 To ub - 1
Debug.Print FormatValue(Text1.Text, NumberToBinary(i, intMaskLen))
Next
End Sub
Private Function NumberToBinary(ByVal sInput As Long, ByVal iMaskLen) As String
Dim lngTemp As Long
Dim strBinary As String
Dim strMask As String
Do
lngTemp = sInput Mod 2
strBinary = CStr(lngTemp) & strBinary
sInput = sInput \ 2
Loop Until sInput = 0
strBinary = String(iMaskLen, "0") & strBinary
strBinary = Right(strBinary, iMaskLen)
NumberToBinary = strBinary
End Function
Private Function FormatValue(ByVal strInput As String, ByVal strMask As String) As String
Dim i As Integer
Dim strRet As String
For i = 1 To Len(strInput)
strRet = strRet & Mid(strInput, i, 1)
If i <= Len(strMask) Then
If Mid(strMask, i, 1) = 1 Then
strRet = strRet & "."
End If
End If
Next i
FormatValue = strRet
End Function
Re: [HELP] String Manipulation
This works, I used a list box rather than ListView. You should be able to easily modify it for adding to a ListView instead:
Code:
Dim ListPoint As Integer, Pointer As Integer, StrLn
RootStr = "vbforums"
' Establish initial list elements with one period first
For I = 1 To Len(RootStr) - 1
List1.AddItem Left$(RootStr, I) & "." & Mid$(RootStr, I + 1)
Next
' Now add elements to the list iteratively based on the initial list
For I = 2 To Len(RootStr) - 1
For J = I To Len(RootStr) - 1
If ListPoint Then
List1.AddItem Left$(List1.List(ListPoint), J + StrLn + 1) & "." & Mid$(List1.List(ListPoint + 1), StrLn + J + 2)
Else: List1.AddItem Left$(List1.List(ListPoint), J + StrLn + 1) & "." & Mid$(List1.List(ListPoint), StrLn + J + 2)
End If
Pointer = Pointer + 1
Next
' Increment List Pointer and String Length
ListPoint = ListPoint + Pointer + 1
StrLn = StrLn + 1
Pointer = 0 ' Reset Pointer to zero
Next
Rather simple, don't you think?
Re: [HELP] String Manipulation
Hey Doc - Your code misses a few possibilities. If your input string is TEST, the result set misses TE.S.T
Re: [HELP] String Manipulation
So it does. Generalizing for any string length is a challenge.
OK. I'll work on it. Seems like a simple fix once the pattern is established.
Re: [HELP] String Manipulation
Quote:
Originally Posted by
MarkT
Hey Doc - Your code misses a few possibilities. If your input string is TEST, the result set misses TE.S.T
Mark, I think I got it right this time, I love a challenge. This was a dandy:
Code:
Dim XX As Integer, ListPoint As Integer, OffSet As Integer
MyStr = "vbforums"
'Build an Initial List
For I = 1 To Len(MyStr) - 1
List1.AddItem Left$(MyStr, I) & "." & Mid$(MyStr, I + 1)
Next
' Add non-adjacent periods to each string in the list iteratively
Do While ListPoint < List1.ListCount - 1
J = J + 1
OffSet = J
For I = ListPoint To List1.ListCount - 2
XX = InStr(J + OffSet, List1.List(I), ".")
If XX Then
List1.AddItem Left$(List1.List(I), XX + 1) & "." & Mid$(List1.List(I), XX + 2)
OffSet = OffSet + 1
End If
Next
ListPoint = I + 1
Loop
I've tested this with strings of different lengths and it appears to work.
Re: [HELP] String Manipulation
Keep trying
Using 12345 as your test string see if you have these
12.34.5
1.234.5
1.23.45
1.23.4.5
1.2.34.5
When the string reaches 8 letters you have 99 misses.
Re: [HELP] String Manipulation
Quote:
Originally Posted by
MarkT
Keep trying
Using 12345 as your test string see if you have these
12.34.5
1.234.5
1.23.45
1.23.4.5
1.2.34.5
When the string reaches 8 letters you have 99 misses.
Using the code in post #9, I get these using 12345 as a string:
1.2345
12.345
123.45
1234.5
1.2.345
12.3.45
123.4.5
1.2.3.45
12.3.4.5
1.2.3.4.5
That's not the same as what you posted, but I guess you were listing only potential exceptions. It that true? I assumed that the periods were supposed to be separated by at most one character and never adjacent to each other. Was that an incorrect assumption?
Re: [HELP] String Manipulation
My take on it was the periods could be anywhere or everywhere in the string.
Re: [HELP] String Manipulation
Quote:
Originally Posted by
MarkT
My take on it was the periods could be anywhere or everywhere in the string.
... provided they were separated by a character? No two or more periods could be adjacent--correct?
Now that I look back, OP indicated in his list that at least these were required: v.bf.orums and v.bfor.ums
So I guess any two periods could be separated by as many as n - 2 characters, but they must be separated by at least one. My code in post #9 restricts the period separation to no more than 1 character. That could be modified. Regardless, I think we need a clarification from OP before devoting anymore time to this. :ehh:
Re: [HELP] String Manipulation
Quote:
Originally Posted by
Code Doc
Mark, I think I got it right this time, I love a challenge. This was a dandy:
Code:
Dim XX As Integer, ListPoint As Integer, OffSet As Integer
MyStr = "vbforums"
'Build an Initial List
For I = 1 To Len(MyStr) - 1
List1.AddItem Left$(MyStr, I) & "." & Mid$(MyStr, I + 1)
Next
' Add non-adjacent periods to each string in the list iteratively
Do While ListPoint < List1.ListCount - 1
J = J + 1
OffSet = J
For I = ListPoint To List1.ListCount - 2
XX = InStr(J + OffSet, List1.List(I), ".")
If XX Then
List1.AddItem Left$(List1.List(I), XX + 1) & "." & Mid$(List1.List(I), XX + 2)
OffSet = OffSet + 1
End If
Next
ListPoint = I + 1
Loop
I've tested this with strings of different lengths and it appears to work.
bro,i do not know u will get my reply or not.
by this code,"vbforums" get 28 combination,but i get 128 variation of it in a php site
can you make your function more that gets more result?????
Re: [HELP] String Manipulation
Quote:
Originally Posted by
MarkT
Based on Lenggries' idea, this should work.
Code:
Option Explicit
Private Sub Command1_Click()
Dim i As Integer
Dim ub As Integer
Dim intMaskLen As Integer
intMaskLen = Len(Text1.Text) - 1
ub = 2 ^ intMaskLen
For i = 1 To ub - 1
Debug.Print FormatValue(Text1.Text, NumberToBinary(i, intMaskLen))
Next
End Sub
Private Function NumberToBinary(ByVal sInput As Long, ByVal iMaskLen) As String
Dim lngTemp As Long
Dim strBinary As String
Dim strMask As String
Do
lngTemp = sInput Mod 2
strBinary = CStr(lngTemp) & strBinary
sInput = sInput \ 2
Loop Until sInput = 0
strBinary = String(iMaskLen, "0") & strBinary
strBinary = Right(strBinary, iMaskLen)
NumberToBinary = strBinary
End Function
Private Function FormatValue(ByVal strInput As String, ByVal strMask As String) As String
Dim i As Integer
Dim strRet As String
For i = 1 To Len(strInput)
strRet = strRet & Mid(strInput, i, 1)
If i <= Len(strMask) Then
If Mid(strMask, i, 1) = 1 Then
strRet = strRet & "."
End If
End If
Next i
FormatValue = strRet
End Function
ya,this one is best