Left, Mid, Right text functions

Post your requests and ideas on the future development of mikroBasic.
Post Reply
Author
Message
Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

Left, Mid, Right text functions

#1 Post by Kalain » 09 Nov 2006 19:49

Hi,

It would be nice to add functions like :
- Left(length, text) : extract length characters from text from left
- Mid(start, length, text)
- Right(length, text)

I have written some procedures but can't find equivalent code with functions. :shock:
A built in library would be certainly more compact than what I wrote.

Here is the code for those who are interested :

Code: Select all

sub procedure str_left(dim nb_char as byte, dim byref text as string[255], dim byref result_txt as string[255])
  dim i as byte
  for i = 0 to nb_char - 1
    result_txt[i] = text[i]
  next i
end sub

sub procedure str_mid(dim start, nb_char as byte, dim byref text as string[255], dim byref result_txt as string[255])
  dim i,j as byte
  j = start-1
  for i = 0 to nb_char - 1
    result_txt[i] = text[j+i]
  next i
end sub

sub procedure str_right(dim nb_char as byte, dim byref text as string[255], dim byref result_txt as string[255])
  dim i,j as byte
  j = strlen(text)-nb_char
  for i = 0 to nb_char - 1
    result_txt[i] = text[j+i]
  next i
end sub

dim text1 as string[20]
dim txtresult as string[20]

main:

  text1 = "AZERTYUIOPQSDFGHJKLM"

  str_left(5,text1,txtresult)    'will return "AZERTY" in txtresult
  txtresult = "     "
  str_mid(7,3,text1,txtresult)  'will retrun "UIO" in txtresult
  txtresult = "     "
  str_right(3,text1,txtresult)  ''will retrun "KLM" in txtresult

In my procedures, first text character is nro 1. (not 0)
Alain

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

Re: Left, Mid, Right text functions

#2 Post by zristic » 10 Nov 2006 09:33

Can we modify and include these routines in the next release?

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

Re: Left, Mid, Right text functions

#3 Post by Kalain » 10 Nov 2006 14:09

Hi,
zristic wrote:Can we modify and include these routines in the next release?
Yes you can, I think that a function is easier to understand and manipulate, this is more in the way to think or in Basic way but I couldn't find the way to write it in Functions. MB returns me some errors message with variables declarations.

Anyway, here is what would be better :

Code: Select all

  text1 = "AZERTYUIOPQSDFGHJKLM" 

  txtresult = str_left(5,text1)    'will return "AZERTY" in txtresult 
  txtresult = "     " 
  txtresult = str_mid(7,3,text1)  'will return "UIO" in txtresult 
  txtresult = "     " 
  txtresult = str_right(3,text1)  ''will return "KLM" in txtresult 
Alain

exemcs
Posts: 25
Joined: 13 Aug 2006 00:27
Location: Herefordshire, UK

#4 Post by exemcs » 11 Nov 2006 00:44

I would much prefer the function parameters to be in the sequence:

string,n1[,n2]

like all the Microsoft Basic versions I use.

The MS mid function has an optional value for the length of the string and can be omitted if you just want all the characters from position n1 onwards.

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

#5 Post by Kalain » 11 Nov 2006 09:24

exemcs wrote:
string,n1[,n2]
....
The MS mid function has an optional value for the length of the string and can be omitted if you just want all the characters from position n1 onwards.
Yes, you're right this follow the same rule (for arguments) :wink: to use right and mid function.l
Alain

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

Re: Left, Mid, Right text functions

#6 Post by Kalain » 05 Oct 2007 21:37

zristic wrote:Can we modify and include these routines in the next release?
Hi zoran,

Which release ?
mb 6.0.0.0 or later ? :cry:
Alain

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

Re: Left, Mid, Right text functions

#7 Post by zristic » 08 Oct 2007 09:03

Kalain wrote:Which release ?
mb 6.0.0.0 or later ?
As soon as we find time for that.

Post Reply

Return to “mikroBasic Wish List”