VBScript - Insert text into the middle of a string

Asked By Hazeus Barbarius
18-Nov-08 01:30 PM
Earn up to 0 extra points for answering this tough question.

I am reading a text file line by line checking for specific criteria..

Do While objFile.AtEndOfStream <> True
  strCurrentLine = objFile.ReadLine
if InStr(226, strCurrentLine, "7141570", vbTextCompare) = 226 Then

>Checking Column 226 for this text "7141570", when this returns TRUE I want to insert "99" elsewhere on that line (lets say Column 250).  How do I do this?

 

  Solution

san san replied to Hazeus Barbarius
18-Nov-08 02:31 PM
Hi

Try try the bellow code.. It might help you out....


  strCurrentLine = objFile.ReadLine
If Not InStr(226, strCurrentLine, "7141570", 1) = 0 Then
  ' appent 99 at last
  strCurrentLine = strCurrentLine && "99"
End If


How it works?
Take a look at the Instr function
InStr([start,]string1,string2[,compare])

start --> Optional. Specifies the starting position for each search. The search begins at the first character position by default. This parameter is required if compare is specified
string1 --> Required. The string to be searched
string2 --> Required. The string expression to search for
compare --> Optional. Specifies the string comparison to use. Default is 0

Can have one of the following values:

0 = vbBinaryCompare - Perform a binary comparison
1 = vbTextCompare - Perform a textual comparison
-----------
Hope it helps

  use replace method for that

Web Star replied to Hazeus Barbarius
18-Nov-08 11:24 PM

as follows

strCurrentLine = objFile.ReadLine
If Not InStr(226, strCurrentLine, "7141570", 1) = 0 Then
  'here u replace the "7141570" string with "99" as u wnat
  strCurrentLine = strCurrentLine.Replace('7141570','99')
End If

  try this link

C_A P replied to Hazeus Barbarius
19-Nov-08 03:56 AM

Insert String Function
String Manipulations Class

Public Function InsertString( _
ByVal vDest As Variant _
, ByVal vSource As Variant _
, Optional ByVal vInsertPosition As Variant _
) As Variant

Insert one string into the middle of another string and return the results.
vSource is inserted into vDest before character position vInsertPosition.

Summary: Similar to the StrIns subroutine, except that this is a function which returns the result instead of modifying an argument.
Example:
    InsertString("123456", "NEW", 3) = "12NEW3456"
See also:
    OverlayString Function
DeleteString Function
StrIns Subroutine
Function returns Null if both strings (vDest and vSource) are Null.

vDest: The string into which the other string is to be inserted. vDest defaults to an empty string if it is Null or cannot be fixed up to a String.

vSource: The string which is to be inserted into the other string. vSource defaults to an empty string if it is Null or cannot be fixed up to a String.



http://www.tek-tips.com/viewthread.cfm?qid=929958
http://p2p.wrox.com/topic.asp?TOPIC_ID=75032

Create New Account