Solved

VB.Net - Remove Text Between Characters

Posted on 2015-02-09
9
213 Views
Last Modified: 2015-02-11
Good Day Experts!

I have quite the little project here today.  I need to remove text between 2 characters and also remove the 2 characters.  This happens 2 times in a line.  It "feels" like it is fairly attainable but I cannot seem to find a solution.  Here is a sample of what I am looking at:

GBP       British Pound       0.6528 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=bdcf3f2cf0&e=a2e1cf5770>        1.5318 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=47712730df&e=a2e1cf5770>       

This is all 1 line in Textpad. I need to remove the text between both sets of <> as well as the <>. So I need to be left with:

GBP       British Pound       0.6528 1.5318

Is this a pretty straight forward endeavor? Can you help me get started with a few tips?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
9 Comments
 
LVL 13

Accepted Solution

by:
LIONKING earned 200 total points
ID: 40598572
Quick and dirty way... You can find the substring by finding the < and > symbols, then do that two times (one for each link).

I'm sure there's a neater and more elegant way, I'm just typing this out of the top of my head.
0
 
LVL 33

Assisted Solution

by:it_saige
it_saige earned 100 total points
ID: 40598631
An implementation of a quick and dirty method:
Module Module1
	Sub Main()
		Dim gbp = "GBP       British Pound       0.6528 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=bdcf3f2cf0&e=a2e1cf5770>        1.5318 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=47712730df&e=a2e1cf5770>"
		Dim index As Integer = gbp.IndexOf("<")
		While (index <> -1)
			If gbp.IndexOf(">", index) <> -1 Then
				gbp = String.Format("{0}{1}", gbp.Substring(0, index), gbp.Substring(gbp.IndexOf(">", index) + 1))
			End If
			index = gbp.IndexOf("<", index + 1)
		End While
		Console.WriteLine(gbp)
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPG-saige-
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 40598641
Hi jimbo99999;

This should do what you need.
Imports System.Text.RegularExpressions


'' Test Data
Dim data = "GBP       British Pound       0.6528 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=bdcf3f2cf0&e=a2e1cf5770>        1.5318 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=47712730df&e=a2e1cf5770>      "
'' Remove URL's from the string and trim any white spaces from the start and end
data = Regex.Replace(data, "<http[^>]*>\s*", String.Empty).Trim()

Open in new window


Resulting string.
GBP       British Pound       0.6528 1.5318
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40598656
A change to the substring code presented above:
Module Module1
	Sub Main()
		Dim gbp = "GBP       British Pound       0.6528 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=bdcf3f2cf0&e=a2e1cf5770>        1.5318 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=47712730df&e=a2e1cf5770>"
		Dim index As Integer = gbp.IndexOf("<")
		While (index <> -1)
			If gbp.IndexOf(">", index) <> -1 Then
				gbp = String.Format("{0}{1}", gbp.Substring(0, index), gbp.Substring(gbp.IndexOf(">", index) + 1))
			End If
			' Have to ensure that the index does not surpass the length
			index = gbp.IndexOf("<", If(index + 1 > gbp.Length, 0, index + 1)) 
		End While
		Console.WriteLine(gbp)
		Console.ReadLine()
	End Sub
End Module

Open in new window


Otherwise the output is the same.

-saige-
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.

 
LVL 33

Expert Comment

by:it_saige
ID: 40598659
@Fernando, nice regex.  ;)

-saige-
0
 
LVL 22

Assisted Solution

by:rspahitz
rspahitz earned 100 total points
ID: 40598660
Regex could probably handle this well, but I'm not very versed in that.  I'd simply do this:

(assuming that text is stored in a variable called conversion:
Dim iLeftBracketPosn As Integer
Dim iRightBracketPosn As Integer 
Do
   iLeftBracketPosn = conversion.IndexOf("<")
   iRightBracketPosn = conversion.IndexOf(">")
   If iLeftBracketPosn < 0 Or iRightBracketPosn < 0 Then
      Exit Do
   End If
   conversion = conversion.Substring(0, iLeftBracketPosn - 1) & conversion.Substring(iRightBracketPosn + 1)
Loop

Open in new window


(Do a quick check on that to ensure that the left and right brackets are removed and the adjacent text is intact; otherwise you may need to +/- 1 to fix it.)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40598743
If you are attempting to do this wholly in TextPad, then Fernando's regex should work there to. Just be sure to check the "Regular expression" box and leave the Replace box blank in Find/Replace.
0
 

Author Comment

by:Jimbo99999
ID: 40599098
Thanks everyone for the responses.  I will be working on this shortly.
0
 

Author Closing Comment

by:Jimbo99999
ID: 40604185
Thanks everyone for the help.  Lionking spurred my thought process and went went down the path of that suggestion.  I liked the other ways for my toolbox.

Thanks again,
jimbo99999
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
AutoHotkey ActiveX Component Error 8 71
Clone Visual studio 2013 solution 5 24
Get String split 5 48
Programmatically signing Word macros 4 61
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

20 Experts available now in Live!

Get 1:1 Help Now