String concatenation in Julia
String concatenation in Julia is a way of appending two or more strings into a single string whether it is character by character or using some special characters end to end. There are many ways to perform string concatenation.
Example:
Input: str1 = 'Geeks'
str2 = 'for'
str3 = 'Geeks'
Output: 'GeeksforGeeks'
Methods of Concatenation
The different ways in which we can concatenate strings in Julia are :
- Using * operator
- Using ^ operator
- Using string() function
Using ‘*’ operator
It is used to concatenate different strings and/or characters into a single string. We can concatenate two or more strings in Julia using * operator.
Example:
Julia
s1 = "Hello "
s2 = "World !"
s = s1 * s2
print (s)
|
Output:
Using ‘^’ operator
This operator repeats the specified string with the specified number of times. It is used when there is a need to concatenate a single string multiple times.
Example:
Julia
s1 = "Hello "
s = s1 ^ 5
print (s)
|
Output:
Using string() function
Julia provides a pre-defined string() function for the concatenation of strings. This function takes all the strings to be concatenated as arguments and returns a single concatenated string.
string(string1, string2, string3, string4, …)
Example 1:
Julia
s1 = "Hello "
s2 = "World !"
s = string(s1, s2)
print (s)
|
Output:
Example 2:
Julia
s1 = "I"
s2 = "Love"
s3 = "Julia"
string(s1, " " , s2, " " , s3)
print (s)
|
Output:
Storing to a File
Concatenated strings can be stored in a File by using the write() function. Here, firstly the concatenation is done and then the file is first opened into ‘w’ i.e. write mode and the concatenated string is stored with the use of write().
Example:
Julia
s1 = "I"
s2 = "Love"
s3 = "Julia"
s = s1 * " " * s2 * " " * s3
open ( "myfile.txt" , "w" ) do io
write(io, s)
end;
|
Output:
Last Updated :
25 Aug, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...