插入Excel VBAstring

在JAVA或C ++中,我们可以沿着myString.insert(position, word)的行来做一些事情。 有没有办法可以在Excel VBA的string中做同样的事情? 在我的工作表中,我有一个string看起来像这样: 01 / 01 / 995 ,我想插入一年1,所以使它01 / 01 / 1995

 Dim test_date As String test_date = "01 / 25 / 995" test_date = Mid(test_date, 1, 10) & "1" & Mid(test_date, 11, 4) 

有没有更简单/更优雅的方式来做到这一点?

我不认为有一个更干净的方式,所以你可以把它包装在一个函数中。 另一种方法是使用replace ,但它不是更清洁。

 Function Insert(source As String, str As String, i As Integer) As String Insert = Replace(source, tmp, str & Right(source, Len(source)-i)) End Function 

或者只是修改你的

 Function Insert(source As String, str As String, i As Integer) As String Insert = Mid(source, 1, i) & str & Mid(source, i+1, Len(source)-i) End Function