How can I do a line break (line continuation) in Kotlin - syntax

I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?
For example, adding a bunch of strings:
val text = "This " + "is " + "a " + "long " + "long " + "line"

There is no symbol for line continuation in Kotlin. As its grammar allows spaces between almost all symbols, you can just break the statement:
val text = "This " + "is " + "a " +
"long " + "long " + "line"
However, if the first line of the statement is a valid statement, it won't work:
val text = "This " + "is " + "a "
+ "long " + "long " + "line" // syntax error
To avoid such issues when breaking long statements across multiple lines you can use parentheses:
val text = ("This " + "is " + "a "
+ "long " + "long " + "line") // no syntax error
For more information, see Kotlin Grammar.

Another approach is to go for the 3 double quotes "" pairs i.e. press the double quotes 3 times to have something like this.
val text = """
This is a long
long
long
line
""".trimIndent()
With this approach you don't have to use + , \n or escape anything; just please Enter to put the string in the next line.
trimIndent() to format multi-line strings - Detects a common minimal indent of all the input lines, removes it from every line and also removes the first and the last lines if they are blank (notice difference blank vs empty).

Related

When split method doesn't work correctly than I expect?

String s = " hello ";
String[] strs = s.split(" ");
When I run this split method, I see that
Strs[0] = " ";
Strs[1] = " ";
Strs[2] = " ";
Strs[3] = " ";
Which makes sense so far.
However, even there is some " " after the word, "hello", java system did not creates any index for those of " " located after the word, "hello".
So, it eventually ended up to
Strs[0] = " ";
Strs[1] = " ";
Strs[2] = " ";
Strs[3] = " ";
Strs[4] = "hello";
Why is it??
This is a prime example of the two overloading methods of java split.
split(String regex) -
Javadoc
split(String regex, int limit) - Javadoc
When you use s.split(" "), the 2nd parameter limit is considered to be 0 by default, and this eliminates any dangling empty strings from result array.
If you want the dangling empty values, you have to explicitly pass -1 as the parameter like s.split(" ", -1) resulting in [, , , , hello, , ]
The limit parameter actually controls the number of times this pattern " " is applied. The Javadoc documentation links I shared has the technical details for further reading.

Error with If Statment in Liberty/ISE Eiffel

I have a problem with the if statment.
I have a program with commandline-arguments and utf8 (€ - Symbol).
The error is in works_not in the if statement.
class EURO
insert ARGUMENTS
create {ANY}
make
feature {ANY}
make
do
works_not
works
end
works_not
local ok: BOOLEAN
do
print ("%N%NAnzahl Argumente : " + argument_count.to_string + "%N")
print ("%NArgument -> Programmname : " + argument(0))
print ("%NArgument -> Wert : " + argument(1))
print ("%NArgument -> Währung : " + argument(2) + "%N")
ok := argument(2) = "€"
print ("%NArgument(2) ist Euro ? " + ok.to_string + "%N%N")
print ("don't work")
io.put_new_line
if argument(2) = "€" then
euro_in_dm(argument(1).to_real)
else
dm_in_euro(argument(1).to_real)
end
end
works
do
print ("works ")
io.put_new_line
if argument_count /= 2 then
print("%N%N Error (1) %N%N")
else
inspect
argument(2)
when "€" then
euro_in_dm(argument(1).to_real)
when "DM","dm" then
dm_in_euro(argument(1).to_real)
else
print("%N%N Error (2) %N%N")
end
end
end
feature
euro_in_dm (a: REAL)
do
io.put_string("%N Euro -> DM ")
io.put_real(a * 1.95583)
io.put_string("%N%N")
end
dm_in_euro (a: REAL)
do
io.put_string("%N DM -> Euro ")
io.put_real(a / 1.95583)
io.put_string("%N%N")
end
end
The issue is in the comparison operator argument(2) = "€".
In Eiffel strings have a reference type, so the equality operator = compares references to the string objects, not their contents. If you want to compare the string values instead, you need to use an operator ~ that internally calls is_equal after checking that types of both operands are exactly the same, or a more robust version same_string (provided that it is available in your version of the environment). To summarize, you can try one of the following instead of the equality:
argument(2).same_string ("€")
argument(2) ~ "€"
argument(2).is_equal ("€")

strange behaviour when xcode evaluates variables at runtime

I get the error "expression was too complex to be solved in reasonable time" for the following code.
I saw other threads regarding the error with much complexer expressions then mine and it was stated, that the compiler is buggy.
Is this also true for the following simple code ?
Problem:
let value1:Int = 1;
let value2:Int = 3;
var sql="The Number 2 is in "
+ " between " + String(iFrom) + " and " + String(iTo) + ".";
but this works
let value1:Int = 1;
let value2:Int = 3;
var sql="The Number 2 is in "
+ " between " + String(iFrom) + " and " + String(iTo);
The difference is only the "." at the end of concatenation.
If its a bug:
The process SourceKitService is running at max and slowes everything down. Can compilation at runtime be disabled ?
One thing you could try that I've had some success with is to try specifically typing your sql variable.
var sql: String ="The Number 2 is in "
+ " between " + String(iFrom) + " and " + String(iTo) + ".";

How to get the index of a String after the first space in vbsript

I'm trying to grab the first characters before a space.
I know it can be done this way
str = "3 Hello World"
str = Mid(str, 1,2)
But how would i do this after a space?
Edit: Looks like you changed your question to get characters BEFORE the first space instead of AFTER. I've updated my examples.
Here's one way:
strTextBeforeFirstSpace = Split(str, " ")(0)
Assuming a space exists in your string, this would return everything up until the first space.
Another way would be:
strTextBeforeFirstSpace = Left(str, InStr(str, " ") - 1)
You can get the index of the first space with the InStr function
InStr(str, " ")
And use this as a parameter in your Mid function
Dim str, index
str = "3 Hello World"
index = InStr(str," ")
'only neccessary if there is a space
If index > 0 Then
str = Mid(str,1,index - 1)
End If

Remove the desired content from a text

I would like to get a working code to simply remove from a text line a specific part that always begins with "(" and finish with ")".
Sample text : Hello, how are you (it is a question)
I want to remove this part: "(it is a question)" to only keep this message "Hello, how are you"
Lost...
Thanks
One way using Regular Expressions;
input = "Hello, how are you (it is a question)"
dim re: set re = new regexp
with re
.pattern = "\(.*\)\s?" '//anything between () and if present 1 following whitespace
.global = true
input = re.Replace(input, "")
end with
msgbox input
If the part to be removed is always at the end of the string, string operations would work as well:
msg = "Hello, how are you (it is a question)"
pos = InStr(msg, "(")
If pos > 0 Then WScript.Echo Trim(Left(msg, pos-1))
If the sentence always ends with the ( ) section, use the split function:
line = "Hello, how are you (it is a question)"
splitter = split(line,"(") 'splitting the line into 2 sections, using ( as the divider
endStr = splitter(0) 'first section is index 0
MsgBox endStr 'Hello, how are you
If it is in the middle of the sentence, use the split function twice:
line = "Hello, how are you (it is a question) and further on"
splitter = split(line,"(")
strFirst = splitter(0) 'Hello, how are you
splitter1 = split(line,")")
strSecond = splitter1(UBound(Splitter1)) 'and further on
MsgBox strFirst & strSecond 'Hello, how are you and further on
If there is only one instance of "( )" then you could use a '1' in place of the UBound.
Multiple instances I would split the sentence and then break down each section containing the "( )" and concatenate the final sentence.

Resources