Author Topic: Concatenation function  (Read 7309 times)

0 Members and 1 Guest are viewing this topic.

Offline paranoia1001001

  • Newbie
  • Posts: 13
    • View Profile
Concatenation function
« on: April 02, 2021, 10:21:32 am »
Please correct my memory if I am wrong, but wasn't there a concatenate() or concat() function in QBasic? I've been unable to find any information on the QB64 Wiki about any such function in QB64 or QBasic. Am I just remembering another language from long ago?

Marked as best answer by paranoia1001001 on April 02, 2021, 06:30:03 am

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Concatenation function
« Reply #1 on: April 02, 2021, 10:23:19 am »
You must be remembering a different language. Concatenation is done simply using the "+" operator.

For instance

Code: QB64: [Select]
  1. Print "This " + "is " + "my " + "string"
Shuwatch!

Offline paranoia1001001

  • Newbie
  • Posts: 13
    • View Profile
Re: Concatenation function
« Reply #2 on: April 02, 2021, 10:37:20 am »
You must be remembering a different language. Concatenation is done simply using the "+" operator.

For instance

Code: QB64: [Select]
  1. Print "This " + "is " + "my " + "string"

Thank you very much for that confirmation of officially getting old enough to forget something as basic as BASIC. I feel like I've even used it in VBA within Excel, so maybe it just exists in VBA/VB.

If you'll indulge me a moment longer, from what I read from the WIki about concatenation using the "+" operator, it works on string literals, but makes no mention of string variables. Do you happen to know if it works for them as well? Thank you for any information you have. I would simply do a test to find out, but as you know my install isn't working at the moment. I am heading over now to read your response to that post. Thank you, again, and, in advance!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Concatenation function
« Reply #3 on: April 02, 2021, 10:56:23 am »
@paranoia1001001

Yes, you can use the "+" with string variables.
From the Wiki page on STRING:
Quote
Concatenation uses the + addition symbol to add literal or variable parts to a string variable value.
Shuwatch!

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Concatenation function
« Reply #4 on: April 02, 2021, 10:57:53 am »
It works on string expressions, which includes string literals, string variables, and functions that return strings (e.g. CHR$(13) + CHR$(10)).

Offline paranoia1001001

  • Newbie
  • Posts: 13
    • View Profile
Re: Concatenation function
« Reply #5 on: April 02, 2021, 11:31:31 am »
@paranoia1001001

Yes, you can use the "+" with string variables.
From the Wiki page on STRING:

Excellent. Thank you very much!

Offline paranoia1001001

  • Newbie
  • Posts: 13
    • View Profile
Re: Concatenation function
« Reply #6 on: April 02, 2021, 11:32:35 am »
It works on string expressions, which includes string literals, string variables, and functions that return strings (e.g. CHR$(13) + CHR$(10)).

Very helpful information. Your help is kindly appreciated. I obviously need to start reading more carefully, or not at 3 am.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Concatenation function
« Reply #7 on: April 02, 2021, 01:44:52 pm »
Please correct my memory if I am wrong, but wasn't there a concatenate() or concat() function in QBasic? I've been unable to find any information on the QB64 Wiki about any such function in QB64 or QBasic. Am I just remembering another language from long ago?

prob javascript

Code: Javascript: [Select]
  1. var str1 = "I Love";
  2. var str2 = "QB64!";
  3. var res = str1.concat(str2);
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Concatenation function
« Reply #8 on: April 02, 2021, 01:49:31 pm »
At moment, I can't think of any other way to Concatenate strings in QB64?

What am I missing?

FellippeHeitor

  • Guest
Re: Concatenation function
« Reply #9 on: April 02, 2021, 02:02:03 pm »
If you wanna go fancy, cryptic and do unnecessary work, there's MID$:

Code: QB64: [Select]
  1. a$ = "Hello, "
  2. b$ = "World!"
  3. result$ = concat$(a$, b$)
  4. Print result$
  5.  
  6. Function concat$ (str1$, str2$)
  7.     Dim temp$
  8.     temp$ = Space$(Len(str1$) + Len(str2$))
  9.     Mid$(temp$, 1) = str1$
  10.     Mid$(temp$, Len(str1$) + 1) = str2$
  11.     concat$ = temp$

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Concatenation function
« Reply #10 on: April 02, 2021, 02:05:06 pm »
You beat me! same exact thing!

Here is one I just invented:
Code: QB64: [Select]
  1. Print concatenate$("Test", " this.")
  2. Function concatenate$ (a$, b$)
  3.     c$ = Space$(Len(a$) + Len(b$))
  4.     Mid$(c$, 1, Len(a$)) = a$
  5.     Mid$(c$, Len(a$) + 1, Len(b$)) = b$
  6.     concatenate$ = c$
  7.  

All +'s used in the creation of this Function are for numeric adding and not concatenation.

Offline paranoia1001001

  • Newbie
  • Posts: 13
    • View Profile
Re: Concatenation function
« Reply #11 on: April 02, 2021, 04:51:51 pm »
There are obviously some smart people here. OK, if you want a challenge the function (from whatever language it came, thinking VB right now but not sure) was able to take any number of strings as arguments and concatenate them, similar to our very simple tacking on of "+"'s ad infinitum. Have at it!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Concatenation function
« Reply #12 on: April 02, 2021, 04:58:11 pm »
Code: QB64: [Select]
  1.  
  2. Dim As String testarray(1 To 3)
  3.  
  4. testarray(1) = "This is"
  5. testarray(2) = " a "
  6. testarray(3) = "test"
  7.  
  8. Print concat(testarray())
  9.  
  10. Function concat$ (stringarray() As String)
  11.     Dim As String concatenated
  12.     For x = LBound(stringarray) To UBound(stringarray)
  13.         concatenated = concatenated + stringarray(x)
  14.     Next
  15.     concat = concatenated
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Concatenation function
« Reply #13 on: April 02, 2021, 05:06:17 pm »
Code: QB64: [Select]
  1.  
  2. Dim As String testarray(1 To 3)
  3.  
  4. testarray(1) = "This is"
  5. testarray(2) = " a "
  6. testarray(3) = "test"
  7.  
  8. Print concat(testarray())
  9.  
  10. Function concat$ (stringarray() As String)
  11.     Dim As String concatenated
  12.     For x = LBound(stringarray) To UBound(stringarray)
  13.         concatenated = concatenated + stringarray(x)
  14.     Next
  15.     concat = concatenated

Hey @SpriggsySpriggs what are you doing in line 15 of your code?  ;-))

You should consider your overload example.
« Last Edit: April 02, 2021, 05:09:10 pm by bplus »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Concatenation function
« Reply #14 on: April 02, 2021, 05:11:32 pm »
@bplus Nah.
Shuwatch!