LibreOffice Basic: existing utilities for splitting strings? - string

I'm using the LibreOffice Basic language.
I'm wondering if there is any library anywhere I can use for splitting strings into arrays? For example, suppose I have the following string with items separated by an arbitrary number of spaces:
ABC DEF GHI
I'd like to split this string into an array called "item" with the following elements:
item(0) = "ABC"
item(1) = "DEF"
item(2) = "GHI"
I know how to produce these results in LibreOffice Basic using regular expressions or via iterating character-by-character through the original string, but I'm wondering if there are any existing functions or helper utilities I can use, so I don't have to "re-invent the wheel".
Internet searches have not yielded anything, but I could possibly have overlooked something.
Thank you in advance.

It looks like you will need to write your own function. There are several ideas at https://forum.openoffice.org/en/forum/viewtopic.php?f=9&t=33218.
If you will be doing a lot of string manipulation and the project is not too far along yet, then it might be worth considering another UNO-enabled language like Java or Python. In Python the code would be simply:
s = "ABC DEF GHI"
item = s.split()

Related

How to lowercase first letter of each new line in python?

myString = "I Am New To Python,
Trying to learn Different things.
Need your help in this Case."
I want like this:
myString2 = "i Am New To Python,
trying to learn Different things.
need your help in this Case."
How can I do this?
A regular expression substitution matching the first character in a line and replaced with its lower-case version is pretty straightforward:
import re
myString = '''I Am New To Python,
Trying to learn Different things.
Need your help in this Case.'''
print(re.sub(r'^(\s*.)',lambda m: m.group(1).lower(),myString,flags=re.MULTILINE))
Output:
i Am New To Python,
trying to learn Different things.
need your help in this Case.
Note you need to triple-quote your string to be valid. I included the leading white space in the replacement so the result would remove it. Use r'^\s*(.)' to leave it in.
The replacement is an anonymous function that receives the match object of the regular expression.
Using list comprehension you can do like,
myString = '''I Am New To Python,
Trying to learn Different things.
Need your help in this Case.'''
modified_string = '\n'.join([i.strip()[0].lower()+i.strip()[1:] for i in myString.split('\n')])
Hope this helps! Cheers!
If you don't know list comprehension yet, this might be an easier one to understand for beginners.
myString = """I Am New To Python,
Trying to learn Different things.
Need your help in this Case."""
mystring1=myString.splitlines()
mystring2=""
for x in mystring1:
mystring2 = mystring2 + x[0].lower() + x[1:] +"\n"
print(mystring2)

How to compare Strings and put it into another program?

i´ve got small problem and before I spend even more time in trying to solve it i´d like to know if what I want to do is even possible ( and maybe input on how to do it^^).
My problem:
I want to take some text and then split it into different strings at every whitespace (for example "Hello my name is whatever" into "Hello" "my" "name" "is" "whatever").
Then I want to set every string with it´s own variable so that I get something alike to a= "Hello" b= "my" and so on. Then I want to compare the strings with other strings (the idea is to get addresses from applications without having to search through them so I thought I could copy a telephone book to define names and so on) and set matching input to variables like Firstname , LastName and street.
Then, and here comes the "I´d like to know if it´s possible" part I want it to put it into our database, this means I want it to copy the string into a text field and then to go to the next field via tab. I´ve done something like this before with AutoIT but i´ve got no idea how to tell AutoIT whats inside the strings so I guess it must be done through the programm itself.
I´ve got a little bit of experience with c++, python and BATCH files so it would be nice if anyone could tell me if this can even be done using those languages (and I fear C++ can do it and I´m just to stupid to do so).
Thanks in advance.
Splitting a string is very simple, there is usually a built in method called .split() which will help you, the method varies from language to language.
When you've done a split, it will be assigned to an array, you can then use an index to get the variables, for example you'd have:
var str = "Hello, my name is Bob";
var split = str.split(" ");
print split[0]; // is "Hello,"
print split[1]; // is "my" etc
You can also use JSON to return data so you could have an output like
print split["LastName"];
What you're asking for is defiantly possible.
Some links that could be useful:
Split a string in C++?
https://code.google.com/p/cpp-json/

Checking if values in List is part of String

I have a string like this:
val a = "some random test message"
I have a list like this:
val keys = List("hi","random","test")
Now, I want to check whether the string a contains any values from keys. How can we do this using the in built library functions of Scala ?
( I know the way of splitting a to List and then do a check with keys list and then find the solution. But I'm looking a way of solving it more simply using standard library functions.)
Something like this?
keys.exists(a.contains(_))
Or even more idiomatically
keys.exists(a.contains)
The simple case is to test substring containment (as remarked in rarry's answer), e.g.
keys.exists(a.contains(_))
You didn't say whether you actually want to find whole word matches instead. Since rarry's answer assumed you didn't, here's an alternative that assumes you do.
val a = "some random test message"
val words = a.split(" ")
val keys = Set("hi","random","test") // could be a List (see below)
words.exists(keys contains _)
Bear in mind that the list of keys is only efficient for small lists. With a list, the contains method typically scans the entire list linearly until it finds a match or reaches the end.
For larger numbers of items, a set is not only preferable, but also is a more true representation of the information. Sets are typically optimised via hashcodes etc and therefore need less linear searching - or none at all.

Replacing or substituting in a python string does not work

I could almost solve all of my python problems thanks to this great site, however, now I'm on a point where I need some more and specific help.
I have a string fetched from a database which looks like this:
u'\t\t\tcase <<<compute_type>>>:\n\t\t\t\t{\n\t\t\t\t\tif (curr_i <= 1) Messag...
the string is basically plain c code with unix line endings and supposed to be treated in a way that the values of some specific variables are replaced by something else gathered from a Qt UI.
I tried the following to do the replacing:
tmplt.replace(u"<<<compute_type>>>", str(led_coeffs.compute_type))
where 'led_coeffs' is a namedtuple and its value is an integer. I also tried this:
tmplt = Template(u'\t\t\tcase ${compute_type}:\n\t\t\t\t{\n\t\t\t\t\tif (curr_i <= 1) Messag...)
tmplt.substitute(compute_type = str(led_coeffs.compute_type))
however, both approaches do not work and I have no idea why. Finally I was hoping to get some input here. Maybe the whole approach is not right and any hint on how to achieve the replacing in a good manner is highly appreciated.
Thanks,
Ben
str.replace (and other string methods) don't work in-place (string in Python are immutable) - it returns a new string - you will need to assign the result back to the original name for the changes to take effect:
tmplt = tmplt.replace(u"<<<compute_type>>>", str(led_coeffs.compute_type))
You could also invent your own kind of templating:
import re
print re.sub('<<<(.*?)>>>', lambda L, nt=led_coeffs: str(getattr(nt, L.group(1))), your_string)
to automatically lookup attributes on your namedtuple...

MATLAB line continuation within string

In MATLAB, ... is used to continue a line to the next line. But if I want to continue a long string within quotation, what can I do? ... will be treated as a part of the string itself.
Using [] is not a perfect solution since in most cases I use sprintf/fprintf to parse a long string like sql query. Using [] would be cumbersome. thanks.
If you put the string in brackets, you can build it in several pieces:
s = ['abc' 'def' ...
'ghi'];
You can then split that statement into several lines between the strings.
answer=['You can divide strings '...
,'by adding a comma '...
,'(as you probably know one year later).'];
You can use strcat or horzcat, which gives you somewhat more options than [], including the ability to mix in variables along with the hardcoded values.

Resources