帅气的钥匙扣 · 天堂2源码如何汉化 | PingCode智库· 4 周前 · |
不开心的水煮鱼 · python将ip地址转换成整数的方法_py ...· 3 周前 · |
叛逆的长颈鹿 · offset commit failed ...· 2 周前 · |
鬼畜的大象 · 什么编程语言有跨进程读写 • Worktile社区· 3 天前 · |
耍酷的爆米花 · elfinder(开源php网盘) - ...· 2 天前 · |
玩足球的篮球 · 社会学理论_社会学视野网· 4 月前 · |
害羞的帽子 · Social Performance ...· 4 月前 · |
不羁的硬盘 · Debugging NGINX | ...· 8 月前 · |
一身肌肉的荒野 · 斗罗大陆:当初唐三带回来的仙草,为什么没有提 ...· 8 月前 · |
爱喝酒的围巾 · What is Kotlin ...· 11 月前 · |
A
string
is a sequence of characters. In Scala, objects of String are immutable which means a constant and cannot be changed once created.
There are two ways to create a string in Scala:
var str = "Hello! GFG" val str = "Hello! GFG"
var str: String = "Hello! GFG" val str: String = "Hello! GFG"
Note:
If you need to append to the original string, then use
StringBuilder
class.
Example:
// str1 and str2 are two different strings
var
str
1
=
"Hello! GFG"
val
str
2
:
String
=
"GeeksforGeeks"
def
main(args
:
Array[String])
// Display both strings
println(str
1
);
println(str
2
);
An accessor method is those methods which are used to find the information about the object. So, a
length()
method is the accessor method in Scala, which is used to find the length of the given string. Or in other words, length() method returns the number of characters that are present in the string object.
Syntax:
var len1 = str1.length();
Example:
// str1 and str2 are two strings
var
str
1
=
"Hello! GFG"
var
str
2
:
String
=
"GeeksforGeeks"
// Main function
def
main(args
:
Array[String])
// Get the length of str1 and str2 strings
// using length() function
var
LEN
1
=
str
1
.length();
var
LEN
2
=
str
2
.length();
// Display both strings with their length
println(
"String 1:"
+ str
1
+
", Length :"
+ LEN
1
);
println(
"String 2:"
+ str
2
+
", Length :"
+ LEN
2
);
String 1:Hello! GFG, Length :10 String 2:GeeksforGeeks, Length :13
when a new string is created by adding two strings is known as a concatenation of strings. Scala provides
concat()
method to concatenate two strings, this method returns a new string which is created using two strings. You can also use ‘+’ operator to concatenate two strings.
Syntax:
str1.concat(str2);
or
Syntax:
"welcome" + "GFG"
Example:
// str1 and str2 are two strings
var
str
1
=
"Welcome! GeeksforGeeks "
var
str
2
=
" to Portal"
// Main function
def
main(args
:
Array[String])
// concatenate str1 and str2 strings
// using concat() function
var
Newstr
=
str
1
.concat(str
2
);
// Display strings
println(
"String 1:"
+str
1
);
println(
"String 2:"
+str
2
);
println(
"New String :"
+Newstr);
// Concatenate strings using '+' operator
println(
"This is the tutorial"
+
" of Scala language"
+
" on GFG portal"
);
String 1:Welcome! GeeksforGeeks String 2: to Portal New String :Welcome! GeeksforGeeks to Portal This is the tutorial of Scala language on GFG portal
When you required format number or values in your string you will use
printf()
or
format()
methods. Other than these methods, String class also provides a methods named as format() method, this method return a String object instead of PrintStream object.
Example:
帅气的钥匙扣 · 天堂2源码如何汉化 | PingCode智库 4 周前 |
鬼畜的大象 · 什么编程语言有跨进程读写 • Worktile社区 3 天前 |
玩足球的篮球 · 社会学理论_社会学视野网 4 月前 |