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
Solved

Right Statement

Posted on 2001-06-22
17
141 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 10

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 10

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 10

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 10

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 10

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

829 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