• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

Right Statement

I would like to use the right statement and from a file name search for the first space that appears. Once the first space is matched I would like to add an integer behind that space.
Eg: test hello.txt
working from the right hand side, once the space is found I want the file to look like this: test(1) hello.txt

Please can someone help quick.
Thanks
0
mscala
Asked:
mscala
  • 5
  • 5
  • 3
  • +4
1 Solution
 
Z_BeeblebroxCommented:
right(strTest,instr(strTest," ")-1,) & "(1)" & mid(strTest, instr(strTest," ") + 1)

Zaphod.
0
 
mcbethCommented:
Public Function GetLastSpace(pzValue As String) As Integer
Dim x As Integer
Dim nPos As Integer
x = 1
While x > 0
    x = InStr(x, pzValue, " ", vbTextCompare)
    If x > 0 Then
        x = x + 1
        nPos = x
    End If
Wend
GetLastSpace = (nPos - 1)
End Function
0
 
sharmonCommented:
Sub Test()

  Dim strFile As String
  Dim strResult As String
  Dim lngPos As Long
 
  strFile = "test hello.txt"
 
  lngPos = InStrRev(strFile, " ")
 
  If lngPos <> 0 Then
    strResult = Mid$(strFile, 1, lngPos - 1) & _
                "(1)" & _
                Mid$(strFile, lngPos + 1)
  End If
 
  Debug.Print strResult
End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rspahitzCommented:
If you're using VB6, sharmon's code is best because it uses the new InStrRev command.
0
 
aranaCommented:
i think this is what you need (no if's but or thens)


Private Sub Command1_Click()
Dim strtest As String
strtest = "test hello.txt"
strtest = Left(strtest, InStr(strtest, " ") - 1) & "(1)" & Right(strtest, Len(strtest) - InStr(strtest, " "))
MsgBox strtest
End Sub
0
 
aranaCommented:
correction:


(didnt erad the behind that space part)

Private Sub Command1_Click()
Dim strtest As String
strtest = "test hello.txt"
strtest = Left(strtest, InStr(strtest, " ") - 1) & "(1)" & Right(strtest, Len(strtest) - InStr(strtest, " ") + 1)
MsgBox strtest
End Sub
0
 
sharmonCommented:
arana,

Your method would put the (1) in the position of the first space from the left, he wants the first space from the right.  Ontop of that, if he runs it with a string with no spaces in it, it will cause a runtime error.
0
 
aranaCommented:
correction:


(didnt erad the behind that space part)

Private Sub Command1_Click()
Dim strtest As String
strtest = "test hello.txt"
strtest = Left(strtest, InStr(strtest, " ") - 1) & "(1)" & Right(strtest, Len(strtest) - InStr(strtest, " ") + 1)
MsgBox strtest
End Sub
0
 
aranaCommented:
well it gives the same results as the example he used!

but your are right, anyways just change every instr() to a instrrev() and you get the rightmost space.



oh and i dont know if i was the only one who noticed, but he said BEHIND the space? (that means left to the space keeping the space? or replace the space? or to the right of the spaace?)???

if you need to keep the space then sharmon code needs some modification, because it doesnt keep it!
0
 
sharmonCommented:
Yeah, if he can't figure out how to keep the space, he needs more help then this:)  Arana, If you do an inline Instr or InstrRev and the result is 0 and you try to use a string function on it, you will get a runtime error.  Just trying to point it out incase he didn't catch that, not for the silly points that do nothing for me, I'd trade all my points for some lunch about now.
0
 
aranaCommented:
well it gives the same results as the example he used!

but your are right, anyways just change every instr() to a instrrev() and you get the rightmost space.



oh and i dont know if i was the only one who noticed, but he said BEHIND the space? (that means left to the space keeping the space? or replace the space? or to the right of the spaace?)???

if you need to keep the space then sharmon code needs some modification, because it doesnt keep it!
0
 
Richie_SimonettiIT OperationsCommented:
This is not exactly what you want but...

Private Sub Form_Load()
Dim str As String
str = "This has_Two spaces"
MsgBox Replace(str, " ", "(1)", 1, 1) '
End Sub

0
 
sharmonCommented:
To compliment Richie's routine, you could do this...

I wonder if we can get over 100 ways before he closes the question:)

Sub Test()
  Dim str As String
  str = "This has_Two spaces"
  str = StrReverse(str)
  str = Replace(str, " ", ")1(", 1, 1)
  str = StrReverse(str)
  MsgBox str
End Sub
0
 
Richie_SimonettiIT OperationsCommented:
sharmon,
i almost agree with you but, why we would use strreverse?
0
 
sharmonCommented:
I was just being silly, reversed the string so that the Replace would put it in the last space instead of the first....
0
 
Richie_SimonettiIT OperationsCommented:
Oh!
We can talk each other until the end of days, Mr. msacala, are you there?
0
 
mscalaAuthor Commented:
Thanks sharmom

thanks everyone actually. Sorry for the delay but I posted the question at the end of Friday my time. I am in South africa. That's why there was a delay, but thanks a million to everyone.

Thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 5
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now