String manipulation question

P

Paul

Hi,

What would be the best way to remove the following from the start of a
string...

"A1", "A2, "A3", "A4", "A5", "B1, "B2", "B3", "B4", "B5", "C1", "C2", "C3",
"C4", "C5", "D1","D2","D3","D4","D5".

I hope I explained that right. A string will have one of the above strings
as its first two characters and I need them removing from the string.

I tried this...
string1 = replace(string1,"A1","")
string1 = replace(string1,"A2","")
string1 = replace(string1,"A3","")
etc..

and although it works its not very efficient as I would need to do this for
all the above strings!

Cheers,
Paul


Seth Rowe are you out there?!
 
G

Guest

Select case string1.substring(0,2)
case "A1", "A2, "A3", "A4", "A5", "B1, "B2", "B3", "B4", "B5", "C1",
"C2", "C3",
"C4", "C5", "D1","D2","D3","D4","D5"
String1 = string1.substring(2,string1.length-2)
case else
end select
 
A

_AnonCoward

: Hi,
:
: What would be the best way to remove the following from the start of
: a string...
:
: "A1", "A2, "A3", "A4", "A5", "B1, "B2", "B3", "B4", "B5", "C1",
: "C2", "C3", "C4", "C5", "D1","D2","D3","D4","D5".
:
: I hope I explained that right. A string will have one of the above
: strings as its first two characters and I need them removing from
the
: string.
:
: I tried this...
: string1 = replace(string1,"A1","")
: string1 = replace(string1,"A2","")
: string1 = replace(string1,"A3","")
: etc..
:
: and although it works its not very efficient as I would need to do
: this for all the above strings!
:
: Cheers,
: Paul


Several approaches come to mind:


If the strings will *always* start with those values, just strip off
everything that follows:

Newvalue = Mid(OldValue, 3)


If the strings may or may not start with those values, then:

Select Case Left(OldValue, 2)
Case "A1", "A2", "A3", "A4", "A5", _
"B1", "B2", "B3", "B4", "B5", _
"C1", "C2", "C3", "C4", "C5", _
"D1", "D2", "C3", "D4", "D5", _

NewValue = Mid(OldValue, 3)

Case Else

NewValue = OldValue

End Select


Or

If Len(OldValue) < 2 Then
NewValue = OldValue
ElseIf Mid(OldValue, 1, 1) 1 >= "A" AndAlso _
Mid(OldValue, 1, 1) <= "E" Then

If Mid(OldValue, 2, 1) >= "1" AndAlso _
Mid(OldValue, 2, 1) <= "5" Then

NewValue = Mid(OldValue, 3)
Else
NewValue = OldValue
End If
Else
NewValue = OldValue
End If


Or best of all, use a regular expression:

Imports System.Text.RegularExpressions

[...]

Dim Pattern as string = "^[A-Ea-e][1-5].*$"

If Regex.isMatch(OldValue, Pattern) Then
NewValue = Mid(OldValue, 3)
Else
NewValue = OldValue
End If

Ralf
 
C

Cor Ligthert [MVP]

Paul,

You have your answer already but I would probably in this case, use a split
command and than loop through the parts.

Cor
 
P

Pascal

What would be the best way to remove the following from the start of ai will try a loop with string = mid$(string,3, len((string)-3))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top