473,806 Members | 2,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB.Net String MID$ Question - New to vb.net

I am passing in a string of varied length
Example1: mil2345_23.lst
Example2: mil23456_1.lst
Example3: mil5567_1234.ls t

What I need is to parse out all text after the _ (underscore) and before the
..)
From example 1 I need "23" returned
From example2 I need "1" Returned
From Example3 I need "12345" returned

I was think that MID$ would work but I am not sure how to do this.

Thanks in advance for all your help
Nov 20 '05 #1
4 1696
If it always has just one underscore you could do this

Private Function parseFileName(B yVal myString As String) As String
Dim s1 As String() = myString.Split( "_"c)
Dim s2 As String() = s1(1).Split("." c)
Return s2(0)
End Function

--Michael
"Keith Kowalski" <ke***@rdfs.com > wrote in message news:eb******** ******@TK2MSFTN GP11.phx.gbl...
I am passing in a string of varied length
Example1: mil2345_23.lst
Example2: mil23456_1.lst
Example3: mil5567_1234.ls t

What I need is to parse out all text after the _ (underscore) and before the
.)
From example 1 I need "23" returned
From example2 I need "1" Returned
From Example3 I need "12345" returned

I was think that MID$ would work but I am not sure how to do this.

Thanks in advance for all your help

Nov 20 '05 #2
Would this be correct?

Dim strFileName As String = "mil2345_23.lst "

Dim ArrBatchNumberP lus As Array = strFileName.Spl it("_")

Dim strBatchNumber As String = ArrBatchNumberP lus(1).Replace( ".lst",
String.Empty)

It seems to work, but I am not sure if this is the correct way to do this.

Keith Kowalski


"Keith Kowalski" <ke***@rdfs.com > wrote in message
news:eb******** ******@TK2MSFTN GP11.phx.gbl...
I am passing in a string of varied length
Example1: mil2345_23.lst
Example2: mil23456_1.lst
Example3: mil5567_1234.ls t

What I need is to parse out all text after the _ (underscore) and before the .)
From example 1 I need "23" returned
From example2 I need "1" Returned
From Example3 I need "12345" returned

I was think that MID$ would work but I am not sure how to do this.

Thanks in advance for all your help

Nov 20 '05 #3
Keith,
In addition to the other comments:

Is there only one "_"?

Is there only one "."?

Do you want the first "_" or the last "_" before the "."?

Do you want the first "." or the last "." after the "_"?

I would use something like:
Private Shared Function GetStuff(ByVal name As String) As String
Dim index As Integer
index = name.LastIndexO f("_"c)
If index <> -1 Then
name = name.Substring( index + 1)
End If
index = name.IndexOf(". "c)
If index <> -1 Then
name = name.Substring( 0, index)
End If
Return name
End Function

Public Shared Sub Main()
Const Example1 As String = "mil2345_23.lst "
Const Example2 As String = "mil23456_1.lst "
Const Example3 As String = "mil5567_1234.l st"
Const Example4 As String = "mil55671234lst "
Const Example5 As String = "mil55671234.ls t"

Debug.WriteLine (GetStuff(Examp le1), Example1)
Debug.WriteLine (GetStuff(Examp le2), Example2)
Debug.WriteLine (GetStuff(Examp le3), Example3)
Debug.WriteLine (GetStuff(Examp le4), Example4)
Debug.WriteLine (GetStuff(Examp le5), Example5)
Return
End Sub

You can change the LastIndexOf & IndexOf of as appropriate. The "_"c is a
Char literal, as opposed to "_" which is a String literal.

Also normally I use the functions in System.IO.Path to remove parts of a
path, however I didn't here as you wanted a subpart of the file name...

Hope this helps
Jay
"Keith Kowalski" <ke***@rdfs.com > wrote in message
news:eb******** ******@TK2MSFTN GP11.phx.gbl...
I am passing in a string of varied length
Example1: mil2345_23.lst
Example2: mil23456_1.lst
Example3: mil5567_1234.ls t

What I need is to parse out all text after the _ (underscore) and before the .)
From example 1 I need "23" returned
From example2 I need "1" Returned
From Example3 I need "12345" returned

I was think that MID$ would work but I am not sure how to do this.

Thanks in advance for all your help

Nov 20 '05 #4
Yes there is only one _ and only one .

The code that Raterus shows works beatifully, thatnks for all who replied.

Keith Kowalski

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:Ok******** *****@TK2MSFTNG P11.phx.gbl...
Keith,
In addition to the other comments:

Is there only one "_"?

Is there only one "."?

Do you want the first "_" or the last "_" before the "."?

Do you want the first "." or the last "." after the "_"?

I would use something like:
Private Shared Function GetStuff(ByVal name As String) As String
Dim index As Integer
index = name.LastIndexO f("_"c)
If index <> -1 Then
name = name.Substring( index + 1)
End If
index = name.IndexOf(". "c)
If index <> -1 Then
name = name.Substring( 0, index)
End If
Return name
End Function

Public Shared Sub Main()
Const Example1 As String = "mil2345_23.lst "
Const Example2 As String = "mil23456_1.lst "
Const Example3 As String = "mil5567_1234.l st"
Const Example4 As String = "mil55671234lst "
Const Example5 As String = "mil55671234.ls t"

Debug.WriteLine (GetStuff(Examp le1), Example1)
Debug.WriteLine (GetStuff(Examp le2), Example2)
Debug.WriteLine (GetStuff(Examp le3), Example3)
Debug.WriteLine (GetStuff(Examp le4), Example4)
Debug.WriteLine (GetStuff(Examp le5), Example5)
Return
End Sub

You can change the LastIndexOf & IndexOf of as appropriate. The "_"c is a
Char literal, as opposed to "_" which is a String literal.

Also normally I use the functions in System.IO.Path to remove parts of a
path, however I didn't here as you wanted a subpart of the file name...

Hope this helps
Jay
"Keith Kowalski" <ke***@rdfs.com > wrote in message
news:eb******** ******@TK2MSFTN GP11.phx.gbl...
I am passing in a string of varied length
Example1: mil2345_23.lst
Example2: mil23456_1.lst
Example3: mil5567_1234.ls t

What I need is to parse out all text after the _ (underscore) and before

the
.)
From example 1 I need "23" returned
From example2 I need "1" Returned
From Example3 I need "12345" returned

I was think that MID$ would work but I am not sure how to do this.

Thanks in advance for all your help


Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1895
by: Ken Fine | last post by:
No joy on Macromedia's boards after a few days; maybe someone can help me here. I got an excellent string handling function off of planet-source-code.com that converts text strings to proper case, i.e. "the REMAINS of the DAY" is converted to "The Remains Of The Day". This function is particularly good in that it handles certain exceptions. Can you help me make it handle more? I'm interested in two tweaks, one easy, one hard.
4
431
by: Niyazi | last post by:
Hi I am trying to insert some value into SQL Server 2000 tables and I am keep getting the "Input string was not in a correct format" error. When user fills the Form it updates the table call tbl_04_Hellenic. Then In that form some of data also goes and fills another table call tbl_LEHTAR. Here is my code:
5
1625
by: MLH | last post by:
Suppose I have a sentence like "One Two Three Four" and I want it to say "One+Two+Three+Four". Taking into account that there might be more than a single space separating the words sometimes. Is this fairly easy to accomplish within code?
21
3989
by: google | last post by:
I'm trying to implement something that would speed up data entry. I'd like to be able to take a string, and increment ONLY the right-most numerical characters by one. The type structure of the data that is in this field can vary. It's a list of mechanical equipment, and how it is designated varies based on how the customer has them labeled. For example, a list of their equipment might look like: CH-1 CH-2 CH-3
9
6285
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how can i make a fast Right Trim Function in c,using Binary search kind of fast algorithm ? Offcourse...I can use the classical approach too. like : Start from the right most charecter of the string to the left of the
10
2388
by: jt | last post by:
I'm needing to take a binary string start at a certain position and return a pointer from that postion to the end of the binary stirng. something like this: char bstr; char *pos; pos=mid(bstr,35); / *return a pointer of the rest of the binary string starting at element 35 */
6
1494
by: AT | last post by:
Can someone please explain me the following: quote from documentation "Unlike other intrinsic data types, String is a reference type. When a variable of reference type is passed as an argument to a function or subroutine, a reference to the memory address where the data is stored is passed instead of the actual value of the string. " Code
4
1825
by: deancarstens | last post by:
Hi, This is a tougher one, but I'm quite sure someone will have a solution for this. Of course, a last minute thing thrown at me by my boss. I have a unique identifier consisting of regions, districts, a road class value and then sequential numbers, in that order, eg: GA-GAD-C-005 The thing is that the sequential values don't follow onto each other as it does the sorting on the road class value first (which could be A, I or C). So,...
7
3119
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
5
1110
by: Paul | last post by:
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.
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10618
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.