Author |
Topic  |
|
AKazak
Russia
1228 Posts |
Posted - 02/17/2021 : 11:39:02 AM
|
OriginPro 2021b (64-bit) Beta 5 9.8.5.105 Windows 7 Pro x64 SP1
Greetings!
Does LT have a neat command to generate a string by repeating specified characters/stings?
For example, I want to repeat "=" character 13 times and save it to a string variable.
Thank you.
--- Andrey |
|
YimingChen
1684 Posts |
Posted - 02/17/2021 : 3:32:44 PM
|
You can simply define such function with Python and call from LT. Open menu Connectivity->Open Default Python Functions... then put in the code below and save.
def repeat(str, n):
'''s:si'''
return str*n
Then you can try LT script:
py.repeat(=,13)$=;
James |
Edited by - YimingChen on 02/17/2021 3:32:57 PM |
 |
|
AKazak
Russia
1228 Posts |
Posted - 02/17/2021 : 3:51:49 PM
|
quote: Originally posted by YimingChen
You can simply define such function with Python and call from LT. Open menu Connectivity->Open Default Python Functions... then put in the code below and save.
def repeat(str, n):
'''s:si'''
return str*n
Then you can try LT script:
py.repeat(=,13)$=;
James
Dear James,
Great solution! What is '''s:si''' ? Also, can I get the same with multiple character core, for example, "a,b,c"?
--- Andrey |
 |
|
YimingChen
1684 Posts |
|
AKazak
Russia
1228 Posts |
|
YimingChen
1684 Posts |
|
AKazak
Russia
1228 Posts |
|
AKazak
Russia
1228 Posts |
|
YimingChen
1684 Posts |
|
AKazak
Russia
1228 Posts |
|
aplotnikov
Germany
170 Posts |
Posted - 07/05/2022 : 11:31:29 AM
|
#include <origin.h>
string repnchar(string strCh, int n) {
char *src=strCh.GetBuffer(1);
int val=(int) src[0];
strCh.ReleaseBuffer();
string strRes;
char *buf=strRes.GetBuffer(n);
memset(buf, val, n);
strRes.ReleaseBuffer();
return strRes;
}
An improved version:
#include <origin.h>
string repnchar(string strCh, int n) {
string strTmp(strCh.GetAt(0), n);
return strTmp;
}
PS. A prophecy: after a while Origin users will be experienced enough with Python to find out that Origin is just an optional environment for Python. :) |
Edited by - aplotnikov on 07/05/2022 5:38:14 PM |
 |
|
AKazak
Russia
1228 Posts |
Posted - 07/06/2022 : 01:06:55 AM
|
quote: Originally posted by aplotnikov
#include <origin.h>
string repnchar(string strCh, int n) {
char *src=strCh.GetBuffer(1);
int val=(int) src[0];
strCh.ReleaseBuffer();
string strRes;
char *buf=strRes.GetBuffer(n);
memset(buf, val, n);
strRes.ReleaseBuffer();
return strRes;
}
An improved version:
#include <origin.h>
string repnchar(string strCh, int n) {
string strTmp(strCh.GetAt(0), n);
return strTmp;
}
PS. A prophecy: after a while Origin users will be experienced enough with Python to find out that Origin is just an optional environment for Python. :)
Great optimization :)
--- Andrey |
 |
|
|
Topic  |
|