Wildcard string search within a string

G

Gerry Viator

Hi all,


I want to remove this [878454]: With a string function? Can I use the Astric
as wild card? If not, how?

Dim txtstr as String = "Print Report for MRN [878454] from B Database"

txtstr = txtstr.Replace("[*]","")

please help
 
J

Jon Skeet [C# MVP]

Gerry Viator said:
I want to remove this [878454]: With a string function? Can I use the Astric
as wild card? If not, how?

Dim txtstr as String = "Print Report for MRN [878454] from B Database"

txtstr = txtstr.Replace("[*]","")

You could either use regular expressions, or find the first index of
the '[', then the first index of ']' after that, and remove that
section. Here's the regex:

using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string test = "Print Report for MRN [878454] from B Database";

Regex regex = new Regex(@"\[.*\]");

Console.WriteLine (regex.Replace(test, ""));
}
}

Note that you end up with a double space in the middle that way.
 
J

John M Deal

You won't be able to do this with the normal replace method, however
here is a link to a sample of how to do this using the regular
expression features of .Net.

http://www.regular-expressions.info/dotnet.html

The code is in C#, but you shouldn't have a real problem translating it
to VB.Net (I'm guessing that you use VB.Net from you original question).

Have A Better One!

John M Deal, MCP
Necessity Software
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top