Open In App

String concatenation in Julia

Last Updated : 25 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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




# creating string 1
s1 = "Hello "
  
# creating string 2
s2 = "World !"
  
# concatenating the strings
s = s1 * s2
  
# printing the concatenated string
print(s)


Output: 

concat-01

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




# creating string
s1 = "Hello "
  
# concatenating the string
s = s1 ^ 5
  
# printing the concatenated string
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




# creating string 1
s1 = "Hello "
  
# creating string 2
s2 = "World !"
  
# concatenating the strings
s = string(s1, s2)
  
# printing the concatenated string
print(s)


Output: 

Example 2: 

Julia




# creating 3 strings
s1 = "I"
s2 = "Love"
s3 = "Julia"
  
# concatenating the strings
string(s1, " ", s2, " ", s3)
  
# printing the concatenated string
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




# creating 3 strings
s1 = "I"
s2 = "Love"
s3 = "Julia"
  
# concatenating the strings
s = s1 * " " * s2 * " " * s3
  
# storing string s in a file
open("myfile.txt", "w") do io
           write(io, s)
     end;


Output: 



Similar Reads

Julia end Keyword | Marking end of blocks in Julia
Keywords in Julia are reserved words that have a pre-defined meaning to the compiler. These keywords can't be used as a variable name. 'end' keyword in Julia is used to mark the end of a block of statements. This block can be of any type like struct, loop, conditional statement, module, etc. Syntax: block_type block_name Statement Statement end Exa
1 min read
Julia function keyword | Create user-defined functions in Julia
Keywords are the reserved words in Julia which have a predefined meaning to the compiler. These keywords are used to reduce the number of lines in code. Keywords in Julia can't be used as variable names. 'function' keyword is used to create user-defined functions in Julia. These functions are reusable codes that can be called from anywhere in the c
1 min read
Julia continue Keyword | Continue iterating to next value of a loop in Julia
Keywords in Julia are predefined words that have a definite meaning to the compiler. These keywords can’t be used to name variables. 'continue' keyword in Julia skips the statement immediately after the continue statement. Whenever the continue keyword is executed, the compiler immediately stops iterating over further values and sends the execution
1 min read
Julia break Keyword | Exiting from a loop in Julia
Keywords in Julia are predefined words that have a definite meaning to the compiler. These keywords can't be used to name variables. 'break' keyword in Julia is used to exit from a loop immediately. Whenever the break keyword is executed, the compiler immediately stops iterating over further values and sends the execution pointer out of the loop. S
1 min read
Julia local Keyword | Creating a local variable in Julia
Keywords in Julia are reserved words whose value is pre-defined to the compiler and can not be changed by the user. These words have a specific meaning and perform their specific operation on execution.'local' keyword in Julia is used to create a variable of a limited scope whose value is local to the scope of the block in which it is defined. Synt
2 min read
Julia global Keyword | Creating a global variable in Julia
Keywords in Julia are reserved words whose value is pre-defined to the compiler and can not be changed by the user. These words have a specific meaning and perform their specific operation on execution. 'global' keyword in Julia is used to access a variable that is defined in the global scope. It makes the variable where it is used as its current s
2 min read
Concatenation of arrays in Julia - cat(), vcat(), hcat() and hvcat() Methods
The cat() is an inbuilt function in julia which is used to concatenate the given input arrays along the specified dimensions in the iterable dims. Syntax: cat(A...; dims=dims) Parameters: A: Specified arrays. A: Specified dimensions. Returns: It returns the concatenated array. Example: # Julia program to illustrate # the use of cat() method # Getti
2 min read
Convert an Integer to a String in Julia - string() Function
The string() is an inbuilt function in julia which is used to convert a specified integer to a string in the given base. Syntax: string(n::Integer; base::Integer, pad::Integer)Parameters: n::Integer: Specified integer.base::Integer: Specified base in which conversion are going to be performed.pad::Integer: It is number of characters present in the
1 min read
Get size of string in Julia - sizeof() Method
The sizeof() is an inbuilt function in julia which is used to return the size of the specified string i.e, returned size will be equal to the number of code units in the string multiplied by the size (in bytes) of one code unit. Syntax: sizeof(str::AbstractString) Parameters: s::AbstractString: Specified string Returns: It returns the size of the s
1 min read
Reverse a string in Julia - reverse() Method
The reverse() is an inbuilt function in julia which is used to return the reverse of the specified string. Syntax: reverse(s::AbstractString) Parameters: a::AbstractString: Specified string Returns: It returns the reverse of the specified string. Example 1: # Julia program to illustrate # the use of String reverse() method # Getting the reverse of
1 min read
Article Tags :