2012-11-02 117 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")) 

所以我在任何地方裝配一個簡單的重用... :)

問候,
丹尼爾

相關問題