scala字符串的拉鏈操作_在Scala中對字符串進行操作

scala字符串的拉鏈操作

Scala字符串操作 (Scala strings operation)

A string is a very important datatype in Scala. This is why there are a lot of operations that can be done on the string object. Since the regular operations like addition, subtraction is not valid for a string, therefore, special operations like concatenation, comparison are defined.

字符串是Scala中非常重要的數據類型。 這就是為什么可以對字符串對象執行許多操作的原因。 由于常規操作(如加法,減法)對于字符串無效,因此,定義了特殊操作(如串聯,比較)。

In this tutorial, we will share some of the important and common string functions.

在本教程中,我們將分享一些重要和通用的字符串函數

1)字符串相等(==) (1) String Equality (==))

The tradition equality operator == is also available in the string. You can use it to find equality of two string and the equality results in a boolean value.

字符串中也可以使用傳統的等于運算符== 。 您可以使用它來查找兩個字符串的相等性,并且相等性會產生布爾值。

    str1 = str2 // this will return either TRUE OR FALSE 

For Example,

例如,

    val str1 = "Include"
val str2 = "Includes"
str1 == str2 // this will be false

2)字符串長度 (2) String Length)

There is an inbuilt function that is used to find the number of characters in a string. It includes all the space that comes it between. It is used to set the limit for string traversal.

有一個內置函數,用于查找字符串中的字符數。 它包括介于兩者之間的所有空間。 用于設置字符串遍歷的限制。

    // this outputs a positive integer denoting the // number of characters in the array. str1.length 

For example,

例如,

    val str1 = "Include help"
str1.length // this will print 12

3)串連音 (3) String concat)

You can concatenate a string on other. It means the string will be appended on the calling string.

您可以在其他字符串上串聯一個字符串。 這意味著該字符串將附加在調用字符串上。

    str1.concat(str2)

for example,

例如,

    val str1 = "Include"
val str2 = "Help"
str1.concat(str2) // This will print IncludeHelp

4)字符串charAt()方法 (4) String charAt() method )

To print the character at a specific index of the string the charAt method is used. The input is a zero-based index and output will be the corresponding character. Will throw an error if the input is greater than the length of the string.

要在字符串的特定索引處打印字符,請使用charAt方法。 輸入是從零開始的索引,輸出將是相應的字符。 如果輸入大于字符串的長度,將引發錯誤。

    str1.charAt(n)

For example,

例如,

    val name = "Include"
name.charAt(4) // This will output u.

5)indexOf()方法 (5) indexOf() method)

The indexOf() method is used to check the index of a character in a string. This method returns an integer which is positive within the length of the string when the character is found in the array otherwise -1 is given as output.

indexOf()方法用于檢查字符串中字符的索引。 當在數組中找到字符時,此方法返回一個整數,該整數在字符串的長度內為正,否則將給出-1作為輸出。

    str1.indexOf("a")

For example,

例如,

    val name = "Include"
name.indexOf("d") // This will output 5.

6)Substring()方法 (6) Substring() method)

The substring method is used to define a substring from the calling string. It makes a new string with the specified part of the string.

substring方法用于從調用字符串中定義一個子字符串。 它使用字符串的指定部分創建一個新字符串。

    str2 =  str1.substring(startIndex , endIndex)

For example,

例如,

    val name = "IncludeHelp is awesome"
name.substring(14 , 21) // This will output awesome.

Example code that uses all these functions

使用所有這些功能的示例代碼

object MyClass {
def main(args: Array[String]) {
val str1 = "include Help "
val str2 = "is awesome"
println("str1 = " + str1)
println("str2 = " + str2)
println("Comparison between str1 and str2 is " + (str1 == str2))
println("The length of str1 is " + str1.length)
println("Concatenating str1 with str2 gives \n " + str1.concat(str2))
println("The character at  index 5 of str2 is" + str2.charAt(5))
println("The index of 'c' in str1 is " + str1.indexOf("c"))
}
}

Output

輸出量

str1 = include Help 
str2 = is awesome
Comparison between str1 and str2 is false
The length of str1 is 13
Concatenating str1 with str2 gives include Help is awesome
The character at  index 5 of str2 ise
The index of 'c' in str1 is 2

翻譯自: https://www.includehelp.com/scala/operation-on-strings-in-scala.aspx

scala字符串的拉鏈操作

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377573.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377573.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377573.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

九、池化層

一、Pooling layers Pooling layers官網文檔 MaxPool最大池化層下采樣 MaxUnpool最大池化層上采樣 AvgPool最大池化層平均采樣 例如:池化核為(3,3),輸入圖像為(5,5),步長為1,不加邊 最大池化就是選出在池化核為單位圖像中的最大…

[分享]SharePoint移動設備解決方案

老外寫的一個PPT,講SharePoint在移動領域的應用,2012年最新的,有iPad喲。/Files/zhaojunqi/SharePoint2010andMobileDevices.pdf 轉載于:https://www.cnblogs.com/zhaojunqi/archive/2012/04/12/2444712.html

十、非線性激活函數

一、ReLU torch.nn.ReLU(inplaceFalse)官網提供的API 其中inplace表示是否在對原始數據進行替換 由函數圖可以看出,負數通過ReLU之后會變成0,正數則不發生變化 例如:input -1,若inplace True,表示對原始輸入數據進…

最短公共子序列_最短公共超序列

最短公共子序列Problem statement: 問題陳述: Given two strings, you have to find the shortest common super sequence between them and print the length of the super sequence. 給定兩個字符串,您必須找到它們之間最短的公共超級序列&#xff0c…

單調棧 leetcode整理(二)

目錄為什么單調棧的時間復雜度是O(n)496. 下一個更大元素 I方法一:暴力方法二:單調棧哈希表739. 每日溫度單調棧模版解優化503. 下一個更大元素 II單調棧循環遍歷為什么單調棧的時間復雜度是O(n) 盡管for 循環里面還有while 循環,但是里面的while最多執…

Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解決辦法)

ZZ:http://www.blogjava.net/anchor110/articles/355699.html1、在工程下新建lib文件夾,將需要的第三方包拷貝進來。2、將引用的第三方包,添加進工作的build path。3、(關鍵的一步)將lib設為源文件夾。如果不設置&…

QTP自傳之web常用對象

隨著科技的進步,“下載-安裝-運行”這經典的三步曲已離我們遠去。web應用的高速發展,改變了我們的思維和生活習慣,同時也使web方面的自動化測試越來越重要。今天,介紹一下我對web對象的識別,為以后的對象庫編程打下基礎…

leetcode中使用c++需要注意的點以及各類容器的初始化、常用成員函數

目錄1、傳引用2、vector使用初始化方法常用成員函數3、字符串string初始化方法常用成員函數4、哈希表 unordered_map初始化常用成員函數示例:計數器5、哈希集合 unordered_set初始化常用成員函數6、隊列 queue初始化成員函數7、棧stack初始化常用成員函數7、emplace…

Linq list 排序,Dictionary 排序

C# 對List成員排序的簡單方法 http://blog.csdn.net/wanzhuan2010/article/details/6205884 LINQ之路系列博客導航 http://www.cnblogs.com/lifepoem/archive/2011/12/16/2288017.html 用一句Linq把一個集合的屬性值根據條件改了,其他值不變 list去重 list.Where((x…

javascript Ajax 同步請求與異步請求的問題

先來看以下代碼: var flagtrue; var index0; $.ajax({url: "http://www.baidu.com/",success: function(data){flagfalse;} }); while(flag){index; } alert(index); 請問最后alert的index的結果是多少? 可能有人會說0唄。實際上卻沒那么簡單…

定義類的Python示例

The task to define a class in Python. 在Python中定義類的任務。 Here, we are defining a class named Number with an attribute num, initializing it with a value 123, then creating two objects N1 and N2 and finally, printing the objects memory locations and a…

十一、線性層

一、Linear Layers torch.nn.Linear(in_features, out_features, biasTrue, deviceNone, dtypeNone) 以VGG神經網絡為例,Linear Layers可以將特征圖的大小進行變換由(1,1,4096)轉換為(1,1,1000) 二、torch.nn.Linear實戰 將CIFAR-10數據集中的測試集二維圖像[6…

easyui plugin——etreegrid:CRUD Treegrid

昨天寫了一個koeasyui的同樣的實現,感覺寫的太亂,用起來十分麻煩,于是今天照著edatagrid,寫了一個etreegrid,這樣再用ko綁定就方便多了。 使用很簡單,$(tableId).etreegrid({idField:parentIdField:,treeField:,saveUr…

expr

expr在linux中 是一個功能非常強大的命令。通過學習做一個小小的總結。 1、計算字符串的長度。我們可以用awk中的length(s)進行計算。我們 也可以用echo中的echo ${#string}進行計算,當然也可以expr中的expr length $string 求出字符串的長度。舉 例[rootlocalhost …

leetcode 42. 接雨水 思考分析(暴力、動態規劃、雙指針、單調棧)

目錄題目思路暴力法動態規劃雙指針法單調棧題目 給定 n 個非負整數表示每個寬度為 1 的柱子的高度圖,計算按此排列的柱子,下雨之后能接多少雨水。 輸入:height [0,1,0,2,1,0,1,3,2,1,2,1] 輸出:6 解釋:上面是由數組…

chdir函數_PHP chdir()函數與示例

chdir函數PHP chdir()函數 (PHP chdir() function) The full form of chdir is "Change Directory", the function chdir() is used to change the current working directory. chdir的完整形式是“更改目錄” , 功能chdir()用于更改當前工作目錄。 Synt…

十二、Sequential

一、Sequential介紹 torch.nn.Sequential(*args) 由官網給的Example可以大概了解到Sequential是將多層網絡進行便捷整合,方便可視化以及簡化網絡復雜性 二、復現網絡模型訓練CIFAR-10數據集 這里面有個Hidden units隱藏單元其實就是連個線性層 把隱藏層全部展開整…

1064-快速排序

描述 給定輸入排序元素數目n和相應的n個元素,寫出程序,利用內排序算法中快速排序算法進行排序,并輸出排序最后結果的相應序列。 輸入 共兩行,第一行給出排序元素數目n,第二行給出n個元素,1≤n≤100000&…

社交問答:取代BBS的Web2.0革命

編者按:本文由樂維UP創始人俞越撰寫,你也可以點擊這里關注俞越的新浪微博。 BBS在中國的興起是在95年,之后以驚人的速度發展起來。從2011年開始,國內的問答社區也如當年的BBS一樣,大量涌現快速成長,大體分為…

單調棧 leetcode整理(三)

目錄42. 接雨水思路分析901. 股票價格跨度思路581. 最短無序連續子數組思路一:排序雙指針思路二:單調棧思路三:雙指針(最省時)42. 接雨水 42. 接雨水 給定 n 個非負整數表示每個寬度為 1 的柱子的高度圖,計算按此排列的柱子&…