By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,534 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,534 IT Pros & Developers. It's quick & easy.

String manipulation question

P: n/a
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?!
Nov 10 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
--
Dennis in Houston
"Paul" wrote:
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?!
Nov 10 '06 #2

P: n/a

"Paul" <ia*@home.co.ukwrote in message
news:Ie********************@pipex.net...
: 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
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Nov 11 '06 #3

P: n/a
Fantastic!
Thanks Dennis and Ralf.

"_AnonCoward" <ab****@uvwxyz.comwrote in message
news:QL******************@southeast.rr.com...
>
"Paul" <ia*@home.co.ukwrote in message
news:Ie********************@pipex.net...
: 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
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.


Nov 11 '06 #4

P: n/a
Paul,

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

Cor

"Paul" <ia*@home.co.ukschreef in bericht
news:Ie********************@pipex.net...
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?!


Nov 11 '06 #5

P: n/a
>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 tried this...
string1 = replace(string1,"A1","")
string1 = replace(string1,"A2","")
string1 = replace(string1,"A3","")
etc..
i will try a loop with string = mid$(string,3, len((string)-3))
Nov 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.