Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.Net - Remove Text Between Characters

Posted on 2015-02-09
9
Medium Priority
?
440 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 800 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 35

Assisted Solution

by:it_saige
it_saige earned 400 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

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 35

Expert Comment

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

-saige-
0
 
LVL 22

Assisted Solution

by:rspahitz
rspahitz earned 400 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

Technology Partners: 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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview
Suggested Courses

926 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