Author Topic: On Concatenation  (Read 3130 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
On Concatenation
« on: February 18, 2021, 03:41:47 pm »
Concatenation only works by addition, there is no subtraction aspect to it. You can't for example have

A$ = "The Quick Brown Fox Jumped over the lazy dog"
B$ ="clever cat"
C$="Brown Fox"
New$=A$ - C$ + B$

with the New$ is now reading "The Quick clever cat Jumped over the lazy dog"


FellippeHeitor

  • Guest
Re: On Concatenation
« Reply #1 on: February 18, 2021, 03:51:49 pm »
You will have to manipulate your string with more detail/a bit more work than that.

- LEFT$ returns the specified amount of bytes from the left of a string: PRINT LEFT$("Hello, world!", 5) 'outputs Hello
- RIGHT$ returns the specified amount of bytes from the right of a string: PRINT RIGHT$("Hello, world!", 5) 'outputs orld!
- MID$ returns a portion from the middle of a string: PRINT MID$("Hello, world!", 8, 5) 'outputs world
- MID$ can also return from a position in a string until the end if you don't specify length: PRINT MID$("Hello, world!", 8) 'outputs world!
- INSTR allows you to find a substring inside a string: PRINT INSTR("Hello, world!", "w") 'outputs 8, since w is found at the 8th character in that string.
- _INSTRREV does the same as INSTR, but starts looking from the end of the string to the first byte.

Check the example in these wiki pages:
- http://www.qb64.org/wiki/MID$_(statement)
- http://www.qb64.org/wiki/INSTR
« Last Edit: February 18, 2021, 03:55:31 pm by FellippeHeitor »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: On Concatenation
« Reply #2 on: February 18, 2021, 04:32:24 pm »
Thanks Fell.

So if i convert a Scientific Notational value to a string ( ie A = .87659003E-02, A$ = STR$(A)) and I want to search for "E-02" is that the INSTR function that I would use?
Result = INSTR(A$,"E-02")

When I run that code I get zero. The RIGHT$(A$,4) actually comes up with "E-03".

I'm a little stumped on it, brain has ceased.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: On Concatenation
« Reply #3 on: February 18, 2021, 04:57:07 pm »
A simple SubString function (Subtract String):

Code: QB64: [Select]
  1. x$ = "foo": PRINT x$
  2. y$ = "bar": PRINT y$
  3. z$ = x$ + y$: PRINT z$
  4. s$ = SubString(z$, y$): PRINT s$
  5. z$ = x$ + y$ + y$ + x$: PRINT z$
  6. s$ = SubString(z$, x$): PRINT s$
  7. s$ = SubString(s$, x$): PRINT s$
  8.  
  9. FUNCTION SubString$ (a$, b$)
  10.     p = _INSTRREV(a$, b$)
  11.     SubString = LEFT$(a$, p - 1) + MID$(a$, p + LEN(b$))
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: On Concatenation
« Reply #4 on: February 18, 2021, 05:06:12 pm »
Hey Dim, scientific notation for...

A = .87659003E-02

Should be written as... A = 8.7659003E-03

Hence the INSTR problem...

Now use

Code: QB64: [Select]
  1. a = 8.7659003E-03
  2. A$ = LTRIM$(STR$(a))
  3. results = INSTR(A$, "E-03")
  4. PRINT results
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: On Concatenation
« Reply #5 on: February 18, 2021, 05:21:30 pm »
That explains it - thanks guys -

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: On Concatenation
« Reply #6 on: February 18, 2021, 05:25:15 pm »
Pete's right.  (For once!  OMG!!)

The reason why you can't find "E-02" in your search with INSTR is simply because A automatically converts to proper scientific notation.  What you might want here, is something a little more flexible to simply look for the "E" or "D" in your string, like so:

Code: QB64: [Select]
  1. a = .87659003E-02
  2. A$ = STR$(a)
  3. results = INSTR(A$, "E-02")
  4. PRINT results, a, A$
  5.  
  6. r = WISNS(a) 'Since a is a SINGLE type variable, use the SINGLE version of WISN.
  7. PRINT r, LEFT$(_TRIM$(STR$(a)), r - 1), MID$(_TRIM$(STR$(a)), r)
  8.  
  9.  
  10. FUNCTION WISNS (num) 'Where Is Scientic Notation SINGLE
  11.     n$ = _TRIM$(STR$(num))
  12.     WISNS = INSTR(n$, "E")
  13.     IF WISNS = 0 THEN WISNS = INSTR(n$, "D")
  14.     IF WISNS THEN WISNS = WISNS
  15.  
  16. FUNCTION WISND (num) 'Where Is Scientic Notation DOUBLE
  17.     n$ = _TRIM$(STR$(num))
  18.     WISND = INSTR(n$, "E")
  19.     IF WISND = 0 THEN WISND = INSTR(n$, "D")
  20.     IF WISND THEN WISND = WISND
  21.  
  22. FUNCTION WISNF (num) 'Where Is Scientic Notation FLOAT
  23.     n$ = _TRIM$(STR$(num))
  24.     WISNF = INSTR(n$, "E")
  25.     IF WISNF = 0 THEN WISNF = INSTR(n$, "D")
  26.     IF WISNF THEN WISNF = WISNF

Note that I've got 3 different functions here for SINGLE, DOUBLE, and _FLOAT values.  This is because they  store the same value much differently in memory.  What might be 9.2E4 in single might simply be 92000 in double, or float....  You'll want your variable types to match to get the proper results back for you. 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: On Concatenation
« Reply #7 on: February 18, 2021, 05:56:41 pm »
"For once" as in that's where you end up with when you loop past infinity... twice!

Pete :D
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/