Solved

VB.Net - Remove Text Between Characters

Posted on 2015-02-09
9
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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 63

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 34

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
 
LVL 34

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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