?
Solved

Replace String in VB.net

Posted on 2011-04-21
9
Medium Priority
?
365 Views
Last Modified: 2012-08-13
Hi all,

For example, I have a string = "ABCDEFGHIJKLMN"
If the string contains this pattern "DEF?" (i.e. DEF follows by any single character). I will substitute it with an empty string.

The value of the string will be "ABCGHIJKLMN" (the four characters DEF? shall be replaced)

How can I do this in VB.NET? Please advise. Thanks.

0
Comment
Question by:cplau
  • 3
  • 2
  • 2
  • +2
9 Comments
 

Author Comment

by:cplau
ID: 35438781
The value of the string should be "ABCHIJKLMN" (the four characters DEF? shall be replaced)

0
 
LVL 2

Expert Comment

by:kambleamar
ID: 35438834
Dim strText As String = "ABCDEFGHIJKLMN"
Response.Write(strText.Replace("DEF", "@"))

result would be ABC@GHIJKLMN

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35438840
you can use regularExpression for that

Dim testString As String = "ABCDEFGHIJKLMN"
Dim str1 As String = System.Text.RegularExpressions.Regex.Replace(testString, "DEF.?", "")
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:cplau
ID: 35438842
My String should be euqal to "ABCHIJKLMN" in the end....
My sarching pattern is "DEF?". It is "DEF" + any single character.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35438848
@cplau - try the regular expression example !
0
 
LVL 2

Expert Comment

by:kambleamar
ID: 35438890
Use Regular expression that would only help here i think

1.*, which describes "0 or more occurrences",
2.+, which describes "1 or more occurrences", and
3.?, which describes "0 or 1 occurrence".


abc$ abc, 123abc, any string ending with abc  
0
 

Author Comment

by:cplau
ID: 35438966
.? describes "0 or 1 occurrence".

Is there any expression to match extactly 1 occurrence only?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35439252
This one may not be ideal but it works


        Dim str As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        Dim str2 = str.Substring(0, str.IndexOf("DEF")) & str.Substring(str.IndexOf("DEF") + 4, str.Length - (str.IndexOf("DEF") + 4))
        MsgBox(str2)
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 1000 total points
ID: 35443830
@cplay, the pattern is just "DEF." without the ?.

@Dhaest code with the correct pattern:
Dim testString As String = "ABCDEFGHIJKLMN"
Dim str1 As String = System.Text.RegularExpressions.Regex.Replace(testString, "DEF.", "")

Open in new window

0

Featured Post

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.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

755 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