views:

212

answers:

4

Okay this one might be a little tougher. I'm using VB that looks like this:

string = Replace(string.ToLower, chr(63), "A")

But I also want chr(63) = "B" as well, like this:

string = Replace(string.ToLower, chr(63), "B")

My problem is that when chr(63) is at the end of a string I need it to be B, and when it's not the end I need it to be A. I suppose that I can use an if/then/else statement. Is there a way to do this?

Example:

XXXXXchr(63)XXXXX = A

but

XXXXXXXXXXchr(63) = B

Thanks!

+1  A: 
erenon
This is a good answer, but not quite what I need. Rather than length (which I don't know) I need it to be B ONLY if it's at the end of the string. Know what I mean? Example:chr(63) = ? So if the word ends in a ? I need it to be B and anything else I need to be A. Hope this makes sense! Thanks!
Robert
You're not describing the problem well. The string object should know the length, so the above code should work, provided that string is 1-based, but it is pseudo-code so that is ok. Otherwise, if there is no way for your code to tell whether it has found the ? at the end or not, there's no way to do the code either.
Lasse V. Karlsen
+1  A: 
string = Replace(string.ToLower, chr(63), "A", 1, Len(string) - 1)
If Right(string, 1) = chr(63) then
   Mid$(string, Len(string), 1) = 'B'
End if

Update: in response to comment:

Mitch Wheat
You'll have to forgive my lack of knowledge of VB functions, I'm just learning. Does Len(string) also stand for length of the string? and how does the -1 and 1 come into play. I'm very new to this. Thanks!
Robert
yes. Len(string) gives a string's length. Replace has these parameters: Replace$(expression, find, replacewith[, start[, count[, compare]]]) (square brackets denote optional parameters)
Mitch Wheat
Okay so I tried using this method and I ran into a problem. I have 2 errors coming up, both for "Right". It has them underlined and I don't have enough knowledge of what it does to figure out why. Everything else seems to check out. Any clues why it's having this issue?
Robert
updated answer to use Mid$()
Mitch Wheat
Lol okay I understand what right/left mean now thanks to you guys and I get why. However, my VB doesn't seem to recognize Right and even though I changed the 2nd one to mid, the first one is still Right and doesn't allow it to work. Any other suggestions?
Robert
A: 

I haven't used Visual Basic since version 6, but it should be something like this:

If Robert.EndsWith(chr(63)) Then
    Robert = Left(Robert, Robert.Length - 1) + "B"
End If

Then do the usual replacement with A.

Thorarin
Could part of my problem be because I'm using Microsoft Visual Studio 2008?
Robert
A: 

This ought to do it

    Dim s As String 
    Dim char63 As String = Convert.ToChar(63).ToString
    If s.EndsWith(char63) Then
        s = s.Substring(0, s.Length - 1) & "B"
    End If
    s = s.Replace(char63, "A")
Dan F