📜  Julia 中的字符串

📅  最后修改于: 2022-05-13 01:55:11.005000             🧑  作者: Mango

Julia 中的字符串

Julia 中的字符串是一组字符或用双引号 (" ") 括起来的单个字符。它是一个有限的字符序列。 Julia 允许使用方括号 ([ ]) 提取字符串的元素以形成多个子字符串。

创建字符串

Julia 中的字符串可以使用双引号和三引号创建。

# 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) 

输出:
弦乐创作

访问字符中的字符

Julia 允许通过使用索引访问字符串来提取字符。要提取一个字符,只需在方括号 ([]) 中传递索引值或一系列值。访问超出范围的索引将导致 BoundsError。仅允许将整数作为索引传递,浮点数或其他类型也会导致 BoundsError。
弦乐-Julia-01

# 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]) 

输出:
字符串-Julia-Output-02

字符串切片

对字符串进行切片是为了访问字符串中的一系列字符。它是通过使用方括号([])内的切片运算符(冒号)传递一系列索引值来完成的。

# 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]) 

字符串-Julia-Output-03

字符串的连接

字符串的连接是将两个或多个字符串加在一起形成一个字符串的过程。 Julia 中的字符串连接可以通过使用string(str1, str2, ...)函数以非常简单的方式完成。

# 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)

输出:
字符串-Julia-Output-04

字符串插值

字符串插值是在字符串中替换变量值或表达式的过程。这是一个组合字符串的过程,但不使用连接方法。插值基本上是执行字符串中可执行的任何内容的过程。在 Julia 中,美元符号 ($) 用于在字符串中插入变量的值。

# 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)

输出:
字符串-Julia-Output-05

字符串方法

MethodsDescription
* – operatorConcatenates different strings and/or characters into a single string
^ – operatorRepeats 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