Solved

Right Statement

Posted on 2001-06-22
17
143 Views
Last Modified: 2010-05-18
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
Comment
Question by:mscala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +4
17 Comments
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6218547
right(strTest,instr(strTest," ")-1,) & "(1)" & mid(strTest, instr(strTest," ") + 1)

Zaphod.
0
 
LVL 1

Expert Comment

by:mcbeth
ID: 6218619
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
 
LVL 6

Accepted Solution

by:
sharmon earned 100 total points
ID: 6218634
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 22

Expert Comment

by:rspahitz
ID: 6219160
If you're using VB6, sharmon's code is best because it uses the new InStrRev command.
0
 
LVL 11

Expert Comment

by:arana
ID: 6219789
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
 
LVL 11

Expert Comment

by:arana
ID: 6219803
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
 
LVL 6

Expert Comment

by:sharmon
ID: 6219929
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
 
LVL 11

Expert Comment

by:arana
ID: 6220094
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
 
LVL 11

Expert Comment

by:arana
ID: 6220115
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
 
LVL 6

Expert Comment

by:sharmon
ID: 6220163
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
 
LVL 11

Expert Comment

by:arana
ID: 6220216
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6220826
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
 
LVL 6

Expert Comment

by:sharmon
ID: 6220848
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6221782
sharmon,
i almost agree with you but, why we would use strreverse?
0
 
LVL 6

Expert Comment

by:sharmon
ID: 6221850
I was just being silly, reversed the string so that the Replace would put it in the last space instead of the first....
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6221976
Oh!
We can talk each other until the end of days, Mr. msacala, are you there?
0
 

Author Comment

by:mscala
ID: 6223556
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question