如何在VB.NET中删除两个数字之间的冒号?

时间:2021-11-17 13:55:24

I have a text in this format:

我有这种格式的文字:

term: 156:59 datainput

术语:156:59 datainput

I want to remove the ":" between the number and then replace it with something else so the text can become:

我想删除数字之间的“:”,然后用其他东西替换它,以便文本可以变为:

term: 156-59 datainput

术语:156-59 datainput

How can I do this in VB.NET?

我怎么能在VB.NET中这样做?

4 个解决方案

#1


In VB.NET (credit Jonathan):

在VB.NET(信用Jonathan):

    Dim text As String = "term: 156:59 datainput"
    Dim fixedText As String = Regex.Replace(text, "(\d+):(\d+)", "$1-$2")

nb: removed last two lines as suggested.

nb:按建议删除最后两行。

#2


Yes, I know this is a little clumsy, but it should work (assuming your data is in exactly the format you specify):

是的,我知道这有点笨拙,但它应该有效(假设您的数据完全符合您指定的格式):

input[input.IndexOf(":", input.IndexOf(":")+1)] = "#"

Of course, if you want a more general case to find NUMBER:NUMBER and replace it with NUMBER#NUMBER, I'd recommend using a regular expression, like this:

当然,如果您想要更常规的案例来查找NUMBER:NUMBER并将其替换为NUMBER#NUMBER,我建议使用正则表达式,如下所示:

var re = new Regex(@"(\d+):(\d+)");
re.Replace(input, "$1#$2");

#3


Assuming you have read the data in to a string, take a look at the string.replace function.

假设您已将数据读入字符串,请查看string.replace函数。

#4


you can also do this using string replace

你也可以使用字符串替换来做到这一点

Dim data As String = "term: 156:59 datainput"

Dim data As String =“term:156:59 datainput”

data = data.Replace(":", "-").Replace("term-", "term:")

data = data.Replace(“:”,“ - ”)。替换(“term-”,“term:”)

#1


In VB.NET (credit Jonathan):

在VB.NET(信用Jonathan):

    Dim text As String = "term: 156:59 datainput"
    Dim fixedText As String = Regex.Replace(text, "(\d+):(\d+)", "$1-$2")

nb: removed last two lines as suggested.

nb:按建议删除最后两行。

#2


Yes, I know this is a little clumsy, but it should work (assuming your data is in exactly the format you specify):

是的,我知道这有点笨拙,但它应该有效(假设您的数据完全符合您指定的格式):

input[input.IndexOf(":", input.IndexOf(":")+1)] = "#"

Of course, if you want a more general case to find NUMBER:NUMBER and replace it with NUMBER#NUMBER, I'd recommend using a regular expression, like this:

当然,如果您想要更常规的案例来查找NUMBER:NUMBER并将其替换为NUMBER#NUMBER,我建议使用正则表达式,如下所示:

var re = new Regex(@"(\d+):(\d+)");
re.Replace(input, "$1#$2");

#3


Assuming you have read the data in to a string, take a look at the string.replace function.

假设您已将数据读入字符串,请查看string.replace函数。

#4


you can also do this using string replace

你也可以使用字符串替换来做到这一点

Dim data As String = "term: 156:59 datainput"

Dim data As String =“term:156:59 datainput”

data = data.Replace(":", "-").Replace("term-", "term:")

data = data.Replace(“:”,“ - ”)。替换(“term-”,“term:”)