One Stupid Problem

08-19-2007, 10:37 AM
I have two text:
a = abbbbbabbbb
b = bcdhfilhm

I'd like to transform those text in:


Practically to “b” the character corresponds who is under

Look in Attachments

But the problem is that "the position of “a” and “b” is variable"

08-19-2007, 11:15 AM
I'm really not sure if I understand your intent; a couple of more examples could possibly be helpful.

If I understand it correctly, you want to be able to take a string like so:

where X is some replacement indicator character. Then, you want to have some string that has as many characters as replacement characters:
zyxwvutsr (2)

Then, the operation you want to perform is to replace each instance of the replacement character in (1) with the corresponding character in (2)?
azyxwvcutsr (3)

This is relatively simple, but I have a few questions: Is my observation correct? What happens if the number of replacement characters in (1) is not equal to the number of characters in (2)? What on earth is using this kind of silly replacement scheme, is it a programming assignment? If it's something you designed, there is likely a better way to mark the portions of the string you want characters to go in, for example a{5}c{4} would be pretty easy to manage.

08-19-2007, 01:28 PM
about the first question: yes
about the second one I control how many are the characters
about the last one, "yes it's something I designed"

but "aXXXXXcXXXX" can is also "aXXcXcXaXX" or "aXaaaXXXaaaXcXsX" or ......

08-19-2007, 05:29 PM
The way that I'd tackle something like this is to loop through the characters in the string:
AAA would be the string that contains the X's, *'s, wildcards, whatever you call them.

For LV = 0 To AAA.Length - 1
'in here, you compare characters:
'for example (the line below is an example)
If AAA.Chars(LV) = "X"c Then
'the code here would run everytime an X is found in the AAA string.
End If

Next LV

Of course, inside the loop, you have to assign characters somehow: Here's one way

To replace the character at position LV with a different character:

Temp = AAA.Remove(LV, 1) 'Remove the character at position LV
AAA = Temp.Insert(LV, "P") 'And put a P in position LV.

This will turn the character at position LV into a P.

For your case, you'd need to make it a character from the other string:
So you would do something like this (not exactly like this):

Temp = AAA.Remove(LV, 1) 'Remove the character at position LV
AAA = Temp.Insert(LV, BBB.Chars(Ctr)) 'And put a character in position LV.

I put Ctr because you aren't always going to put the same character from the BBB in. You'll have to increment a Ctr everytime you substitute a string.

Dos this get you closer to a solution?

08-19-2007, 11:09 PM
Rather than using Remove and Insert I'd recommend using a StringBuilder. Each call to Remove or Insert is going to cause the framework to have to create a new string, whereas StringBuilder uses a buffer so it is much more efficient.

Out of respect for Iceplug's "gentle nudge" approach, the example below has blanks that need to be filled in where I felt like describing the algorithm wasn't harder than writing the code.

Public Function SpecialReplace(ByVal input As String, ByVal replacementString As String, ByVal wildcard As Char) As String
Dim buffer As New StringBuilder(input.Length)
Dim currentIndex As Integer = 0

For Each inChar As Char In input
If inChar = wildcard Then
' Append the current character from replacementString to the buffer, then increment currentIndex by one
' Append inChar to the buffer
End If

Return buffer.ToString()
End FunctionThe function above, with the appropriate code, will act as illustrated below and avoids all but one reallocation:
AzywxBzywCz = SpecialReplace("AxxxxBxxxCx", "zywxzywz", "x"c")

08-20-2007, 06:15 AM
Dim S As String = "babb abba abba aba abba ab"
Dim M As String = "cdefghilmnopqrstuvz"
Dim k As Object
Dim j As Object = 0

For k = 0 To S.Length - 1
If S(k) = "b" Then
Mid(S, k + 1) = M(j)
j = j + 1
textbox1.text = (S)
textbox2.text = (M)
End If

08-20-2007, 08:31 AM
Your attempt works but, to be blunt, you are using a horrible practice you need to break as soon as possible.

Dim k As Object
Dim j As Object = 0This is VB .NET, not Perl. In a type-safe language we never use Object unless we have to. Both of these variables should have been declared as Integer.

Why? In this case it's fairly harmless and won't cause too many problems. But suppose you have this variable:Dim input As Object and it is declared a few hundred lines of code away from where you put input in it. A few hundred lines you want to use it. What is in our variable input? A string? An integer? Nothing? We can't tell without using methods like GetType, which are expensive in terms of performance. What if you wanted it to only contain integers? What's there to stop some I/O method from accidentally putting a string inside of it?

Furthermore, because of the way .NET allocates memory, using Object for value types such as Integer severely hurts performance. When you assign a value, .NET must first "box" the value in a reference type then store it. To retrieve the value, .NET must "unbox" the value from the reference type. This is expensive. If you don't believe me, run the following code: Dim value As Integer
Dim obj As Object = 0
Dim counter As Integer
Dim iterations As Integer = 1000000

Dim valTime As TimeSpan
Dim objTime As TimeSpan

Dim timer As DateTime = DateTime.Now
For counter = 0 To iterations
Dim valLoop As Integer = value
valLoop = value + 10
value = valLoop + 5
valTime = DateTime.Now.Subtract(timer)

timer = DateTime.Now
For counter = 0 To iterations
Dim objLoop As Object = obj
objLoop = obj + 10
obj = objLoop + 5
objTime = DateTime.Now.Subtract(timer)

MessageBox.Show(String.Format("Value: {0}{1}Object: {2}", valTime.ToString(), Environment.NewLine, objTime.ToString()))Play with the number of iterations and you will find that using [c]Object[/i] is extremely slow, performance decrease of 10x was not uncommon in my trials.

Plus, using Object leads to subtle errors because you are relying upon the compiler to figure out what type of variable you need. It is often right, but if it makes the wrong decision you will have a very subtle bug on your hands.

At the very least declare the variables as Integer. If you are going to be doing lots of replacements and you will use large strings, I strongly suggest investigating using a StringBuffer as well.

08-20-2007, 09:17 AM
Thanks for the suggestion and the aid

I have made as you said using StringBuffer

08-20-2007, 04:53 PM
Rather than using Remove and Insert I'd recommend using a StringBuilder. Each call to Remove or Insert is going to cause the framework to have to create a new string, whereas StringBuilder uses a buffer so it is much more efficient.
I didn't suggest StringBuilder in hopes that it would be easier to follow what's going on...

08-20-2007, 07:21 PM
Fair enough; I have actually always found using an iterative process and a StringBuilder easier than lots of substring manipulations, but I seem to be a special case. I can't figure out why but I just can't wrap my head around what index I'm supposed to use for some reason.

There's also the performance hit though; there's no avoiding that unfortunately. I can't understand why so many programming languages make strings immutable.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum