String before the first number

  • Thread starter Thread starter mm
  • Start date Start date
M

mm

Hi all

I have a string example like this
TSOP65P640X120-20N M 0 0 0 3 9 0 20 1 0

The first number in this case is 6 but it can be between 0 and 9
The first number can be on place between 2 an 6
How do I get the string before the first number (TSOP)

TIA
Mans
 
Hi all

I have a string example like this
TSOP65P640X120-20N M 0 0 0 3 9 0 20 1 0

The first number in this case is 6 but it can be between 0 and 9
The first number can be on place between 2 an 6
How do I get the string before the first number (TSOP)

^[A-Z]{1,5}

will get the first 1 to 5 upper case letters
 
split the string using string split, then read the array and convert toInt32
each char. If it doesn't convert, you are at the end of the string and into
the numbers. The just join the bits back together.
 
Hi,
you can use regulare expression for this purpose. try this one :

^(.*?)\d{1}

it will fetch you first match as subMatch. it is more effective than
manualy cheking for a integer in the string.
 
John Timney ( MVP ) said:
split the string using string split, then read the array and convert toInt32
each char. If it doesn't convert, you are at the end of the string and into
the numbers. The just join the bits back together.

I think you can do better following the split idea: Split the string on
numbers ("0123456789".ToCharArray()) so the stringarray(0) should contain
the string before the first number (of course: you must be sure that the
string doesn't begin with a number!).
 
mm said:
Hi all

I have a string example like this
TSOP65P640X120-20N M 0 0 0 3 9 0 20 1 0

The first number in this case is 6 but it can be between 0 and 9
The first number can be on place between 2 an 6
How do I get the string before the first number (TSOP)

TIA
Mans
Since you say the first number's max position is 6, then this won't take
too many resources
This code does not validate or check for string index out of bounds errors,
but may give you an idea of another approach.::::
'------------------------------------------------------------
Private Function getFirstChars(stringIn as String) as String
Dim stringOut as String = ""
Dim n as integer = 0
Do While not IsNumeric(stringIn.Substring( n, 1))
stringOut &= stringIn.Substring(n,1)
n += 1
Loop
Return stringOut
End Function
'-----------------------------------------Use it like this
strMyString = getFirstChars( strBigString)
 
mm said:
I have a string example like this
TSOP65P640X120-20N M 0 0 0 3 9 0 20 1 0
How do I get the string before the first number (TSOP)

Besides the other proposed approaches, maybe you'd like to try a more
generic one:

Public Function GetPrefix(ByVal Text As String, _
ByVal PrefixList As String) As String
Dim Size As Integer
For Each C As Char In Text
If PrefixList.IndexOf(C) >= 0 Then
Return Text.Substring(0, Size)
End If
Size += 1
Next
Return ""
End Function

Your string example could be parsed like this:

Dim Prefix as String = GetPrefix( _
"TSOP65P640X120-20N M 0 0 0 3 9 0 20 1 0", _
"0123456789")

HTH.

Regards,

Branco.
 

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

Back
Top