Solved

String manipulation in Visual Basic

Posted on 2016-09-27
7
59 Views
Last Modified: 2016-09-27
Hi,
I've been reading about the Split functions but I have a split on two different characters within a text file.  

The string to parse is <FILENAME>123456789.TIF</FILENAME>

I would like to only extract the information between > and </.   I realize this is xml format, but the document is not an XML document.    The length of the value can vary in size.

Does anyone have any examples or can point me in the right direction?   I've not had any luck so far.   Thanks, Michelle
0
Comment
Question by:Michelle Aycoth
  • 4
  • 2
7 Comments
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 41818694
MsgBox Split(Replace(x, ">", "<"), "<")(2)

Where x is your string variable.
1
 
LVL 35

Expert Comment

by:Kimputer
ID: 41818696
If that's really the string (and not just a part of a much bigger line) it would be

Dim original_string = "<FILENAME>123456789.TIF</FILENAME>"
Dim temp = split(original_string,">")(1)
Dim result = split(temp,"<")(0)
'or one step
result = split(split(original_string,">")(1),"<")(0)

Open in new window


If it's part of a bigger line (or not formatted as predictable), use regex instead.
1
 
LVL 46

Expert Comment

by:Martin Liss
ID: 41818700
Some explanation of my code is probably called for. The "Replace" portion of the above changes all ">" to "<" to make what the Split command splits on consistent. Then the (2) portion returns the 3rd entry in the array that Split returns. (0) would be  a blank because there's nothing in front of the first "<" in the modified string, and (1) would be FILENAME.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Michelle Aycoth
ID: 41818730
Thanks for the quick responses.   I didn't realize I could split on a split.   Thought I had to do some sort of substring configuration.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 41818742
I assume you were responding to Kimputer. You don't need to Split on Split. Please see my post ID: 41818694.
1
 

Author Closing Comment

by:Michelle Aycoth
ID: 41818747
Thanks so much for the quick response.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 41818753
You're welcome and I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA filters 2 51
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 42
VB6 - Convert HH:MM into Decimal 8 51
Add and format columns in vb6 7 47
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now