2012-11-02 118 views
0

我还有一个关于visual basic的问题,即我现在用于为windows phone 7.5开发应用程序的语言。我的问题是如何替换字符串中的最后一个字符?示例我有字符串“clyde”,现在我想用'o'替换最后一个字符'e',但我该怎么做?任何帮助将如此赞赏。替换字符串中的最后一个字符

回答

2
String str = "clyde"; 
str = str.Substring(0, str.Length - 1) + 'o'; 

尝试了一些网上VB转换器

Dim str As String = "clyde" 
str = str.Substring(0, str.Length - 1) & "o"C 
+0

这个完美的作品,非常感谢@ nkchandra – clydewinux

0

编辑:现在测试(I忘了补充命名空间

的myString = Microsoft.VisualBasic.Left(myString的,LEN(myString的) - 1)& myNewChar

例如:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
    Dim myString As String 
    Dim myChar As String 
    myString = "clyde" 
    myChar = "o" 
    myString = Microsoft.VisualBasic.Left(myString, Len(myString) - 1) & myChar 
    MsgBox(myString) 
End Sub 
+0

不work.:- ( – clydewinux

+0

请看看我编辑的版本 – Luke94

1

在vb.net脚本:

Dim s As String 

    Sub Main() 
    s = "hello world" 
    s = s.Substring(0, s.Length - 1) & "o" 

    Console.WriteLine(s) 
    Console.ReadLine() 
    End Sub 
+0

嘿,你说得对!@nkch安德拉有正确的解决方案无论如何...修复它 – RoelF

+0

耐心,它需要一点点更新:-) – RoelF

0

我想出了String类的扩展描述>>on CodeProject<<

Imports Microsoft.VisualBasic 
Imports System.Runtime.CompilerServices 

Public Module StringExtensions 

<Extension()> _ 
Public Function ReplaceFirstChar(str As String, ReplaceBy As String) As String 
    Return ReplaceBy & str.Substring(1) 
End Function 

<Extension()> _ 
Public Function ReplaceLastChar(str As String, ReplaceBy As String) As String 
    Return str.Substring(0, str.Length - 1) & ReplaceBy 
End Function 

End Module 

用法:

dim s as String= "xxxxx" 
msgbox (s.ReplaceFirstChar("y")) 
msgbox (s.ReplaceLastchar ("y")) 

所以我在任何地方装配一个简单的重用... :)

问候,
丹尼尔

相关问题