Author Topic: mid statement  (Read 3277 times)

0 Members and 1 Guest are viewing this topic.

  • Guest
Re: mid statement
« Reply #15 on: October 05, 2018, 08:58:46 PM »
With or without an "end" parameter, it should work no matter of the type of string assigned.

This works:

Code: [Select]
dim s as string2 = "1234567890"
dim s2 as string2 = "00"
mid(s, 3) = s2
print s

But this does not:

Code: [Select]
dim s as string2 = "1234567890"
mid(s, 3) = "00"
print s

The assigned string should be converted to the same encoding (ansi or unicode) that the target (s).

Charles Pegge

  • Guest
Re: mid statement
« Reply #16 on: October 06, 2018, 02:36:23 AM »
I think the best solution will be to use these functions, which have the desired parameters and polymorphs:

Code: [Select]

macro midset(cw)
================
  sys ps=strptr s
  sys pw=strptr w
  int ls=cw*len s
  int lw=cw*len w
  i*=cw
  n*=cw
  if i<0 then i=ls+i+cw 'index from right
  i-=cw 'base 0
  if n>lw then n=lw
  if n+i>ls
    n=ls-i
    if n<0 then return
  end if
  copy ps+i,pw,n
end macro

function amid(char*s,int i, int n,char* w)
==========================================
midset(1)
end function

function amid(string s,int i, int n,string w)
=============================================
midset(1)
end function

function wmid(wchar*s,int i, int n,wchar* w)
============================================
midset(2)
end function

function wmid(wstring s,int i, int n,wstring w)
===============================================
midset(2)
end function


string s="1234567890"
amid (s,2,3)="abcd"
print s
'
wstring s="1234567890"
wstring w="abcd"
wmid (s,2,3)=w
print s
'
char s="1234567890"
amid (s,2,3)="abcd"
print s
'
wchar s="1234567890"
wstring w="abcd"
wmid (s,2,3)=w
print s