在VB.NET中替换/删除字符串中的字符 [英] Replace/Remove a Character in a String in VB.NET

查看:1140
本文介绍了在VB.NET中替换/删除字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!请帮我解决一下这个。我想在字符串中做两件事。

首先,例如,字符串的值是file.name.txt,我想将其更改为file.name_txt 。如果有多个。,我想改变最后一个。到_并保留前面的。。

其次,如果我有一个字符串值file.name_txt.enc,我想将最后一个_替换为。并删除从最后一个。开始的字符。

我熟悉字符串的替换和删除方法,但是当重复特定字符时我不知道怎么做我想要替换或删除。

解决方案

使用string.LastIndexOf来识别最后的'dot',然后使用string.Substring来更改它:

  Dim  filename 作为 字符串 =   file.name.txt 
Dim lastDot 作为 整数 = filename.LastIndexOf( C)
filename = filename.Substring( 0 ,lastDot)& _& filename.Substring(lastDot + 1



您可以使用类似的代码执行第二项任务。


Hi! Please help me with this. I want to do two things in a string.
First, for example, the value of the string is "file.name.txt", I'd like to change it to "file.name_txt". If ever there are multiple ".", I'd like to change the last "." to "_" and retain the preceding ".".
Secondly, If I have a string value "file.name_txt.enc", I like to replace the last "_" to "." and remove the characters starting from the last "."
I'm familiar with the replace and remove methods of string but i don't know how to do it when there are repetition of the specific character that I want to replace or remove.

解决方案

Use string.LastIndexOf to ID the final 'dot' and then string.Substring to change it:

Dim filename As String = "file.name.txt"
Dim lastDot As Integer = filename.LastIndexOf("."C)
filename = filename.Substring(0, lastDot) & "_" & filename.Substring(lastDot + 1)


You can use similar code to do the second task.


这篇关于在VB.NET中替换/删除字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆