Strings in Julia
Strings in Julia are a set of characters or a single character that is enclosed within double quotes(” “). It is a finite sequence of characters. Julia allows extracting elements of a string to form multiple substrings with the use of square brackets([ ]).
Creating a String
Strings in Julia can be created using double quotes and triple quotes.
# Julia Program for # Creation of String # Creating a String # with double Quotes String1 = "Welcome to the Geeks World" println( "String with the use of Double Quotes: " ) println(String1) # Creating a String # with triple Quotes String1 = """I'm a Geek and I live in a world of Geeks""" println( "\nString with the use of Triple Quotes: " ) println(String1) # Creating String with triple # Quotes allows multiple lines String1 = """Geeks For Life""" println( "\nCreating a multiline String: " ) println(String1) |
Output:
Accessing characters in Strings
Julia allows extracting characters by accessing a String with the use of Indexing. To extract a character, just pass the index value or a range of values in square brackets([]). While accessing an index out of the range will cause a BoundsError. Only Integers are allowed to be passed as an index, float or other types will also cause a BoundsError.
# Julia Program to Access # characters of String String1 = "GeeksForGeeks" println( "Initial String: " ) println(String1) # Printing First character println( "\nFirst character of String is: " ) println(String1[ 1 ]) # Printing Last character println( "\nLast character of String is: " ) println(String1[end]) |
Output:
Slicing of Strings
Slicing of a String is done to access a range of characters in a String. It is done by passing a range of Index values with the use of a Slicing Operator(colon) within the square brackets([]).
# Julia Program to Access # characters of String String1 = "GeeksForGeeks" println( "Initial String: " ) println(String1) # Printing 1st to 5th character println( "\nSlicing characters from 1-5: " ) println(String1[ 1 : 5 ]) # Printing characters between # 4th and 3rd last character println( "\nSlicing characters between " , "4th and 3rd last character: " ) println(String1[ 4 :end - 2 ]) |
Concatenation of Strings
The concatenation of strings is the process of adding two or more strings together to form one single string. Concatenation of Strings in Julia can be done in a very simple way by using string(str1, str2, ...)
function.
# Julia Program to concatenate # two or more strings # Declaring String 1 String1 = "Geeks" println( "String1: " ) println(String1) # Declaring String 2 String2 = "for" println( "\nString2: " ) println(String2) # Declaring String 3 String3 = "Geeks" println( "\nString3: " ) println(String3) # String concatenation function String4 = string(String1, String2, String3) # Final String after concatenation println( "\nFinal String:" ) println(String4) |
Output:
Interpolation of Strings
Interpolation of Strings is the process of substituting variable values or expressions in a String. It is a process of combining strings but without using the method of concatenation. Interpolation is basically the process of executing whatever that is executable in a string. In Julia, a dollar sign($) is used to insert the value of a variable in the string.
# Julia Program to for # Interpolation of Strings # Declaring a string str1 = "Geek" # Declaring a Number score = 47 # Interpolation of String String = "Hello $str1, your score is $score" # Printing Final String println(String) |
Output:
String Methods
Methods | Description |
---|---|
* – operator | Concatenates different strings and/or characters into a single string |
^ – operator | Repeats the specified string with the specified number of times |
ascii() | Converts a specified string to String-type and also check the presence of ASCII data |
chomp() | Removes a single trailing newline from a string |
chop() | Removes the last character from the specified string |
cmp() | Checks if the two specified strings are having the same length and the character at each index is the same in both strings |
eachmatch() | Used to search for all the matches of the given regular expression r in the specified string s and then returns a iterator over the matches. |
endswith() | Returns true if the specified string ends with the specified suffix value else return false |
findfirst() | Returns the last occurrence of the specified pattern in specified string |
findlast() | Returns the first occurrence of the specified pattern in specified string |
findnext() | Returns the next occurrence of the specified pattern in specified string starting from specified position |
findprev() | Returns the previous occurrence of the specified pattern in specified string starting from specified position |
first() | Returns a string consisting of the first n characters of the specified string |
isvalid() | Returns true if the specified value is valid for its type else returns false |
join() | Joins an array of strings into a single string |
last() | Returns a string consisting of the last n characters of the specified string |
length() | Returns the length of the specified string with desired starting and end position |
lowercase() | Returns a string with all characters converted to lowercase |
lowercasefirst() | Returns a string with first character converted to lowercase |
lstrip() | Used to remove leading characters from the specified string str |
match() | Returns a RegexMatch object containing the match or nothing if the match failed. |
repeat() | Returns a string which is the repetition of specified string with specified number of times |
replace() | Replaces a word or character with the specified string or character |
reverse() | Returns the reverse of the specified string |
rsplit() | Works exactly like split() method but starting from the end of the string |
rstrip() | Used to remove trailing characters from the specified string str |
sizeof() | Returns the size of the specified string |
split() | Splits a specified string into an array of substrings on occurrences of the specified delimiter(s) |
startswith() | Returns true if the specified string start with the specified prefix else return false |
string() | Converts a specified integer to a string in the given base |
strip() | Used to remove leading and trailing characters from the specified string str |
SubString() | Returns a part of the specified parent string |
uppercase() | Returns a string with all characters converted to uppercase |
uppercasefirst() | Returns a string with first character converted to uppercase |
Please Login to comment...