scala 字符串 格式化
Scala字符串格式化方法 (Scala string formatting methods)
Returning a string to the 便宜香港vps output screen that contains variables as well as constant elements of the text that are used in a string. To display the string in a representable form formatting of the string is required. In Scala, There are methods defined to format strings. The formatting of the string is done using two methods
将字符串返回到输出屏幕,该屏幕包含字符串中使用的变量以及文本的常量元素。 为了以可表示的形式显示字符串,需要格式化字符串。 在Scala中,定义了一些格式化字符串的方法。 字符串的格式化使用两种方法完成
format()
格式()
formatted()
formatted()
Example:
例:
We have to return a string in a formatted form so that the final output would be as follows:
我们必须返回格式化格式的字符串,以便最终输出如下:
Manju earns 400000 //or Ramlal earns 78652
To return This output in which there are two variables personname and personsalary. So we need to add the personname first then the text that is to be printed and the personsalary variable at last.
返回此输出,其中有两个变量personname和personalaryary 。 因此,我们需要在倒数第一添加PERSONNAME然后就是要打印的文本和personsalary变量。
Now let’s see how we can return this input using a format and formatted methods?
现在,让我们看看如何使用格式和格式化方法返回此输入?
format()方法 (format() Method)
The format() method in Scala is from the stringlike trait which holds methods for string operations. This method is used to format strings. It accepts parameters and places them on their specific positions.
Scala中的format()方法来自于字符串状特征,该特征包含用于字符串操作的方法。 此方法用于格式化字符串 。 它接受参数并将其放在特定位置。
The positions of variables are defined using ampersand (&) notations.
变量的位置是使用符号(&)符号定义。
&d is used to insert integer values
&d用于插入整数值
&f is used for inserting floating point or double values
&f用于插入浮点或双精度值
&c is used for inserting characters
&c用于插入字符
&s is used for inserting strings
&s用于插入字符串
Syntax:
句法:
var formatted_string = string_a.format(parameters)
Program 1:
程序1:
object MyClass { def main(args: Array[String]) { var output = “%s earns %d in a month!” var employee_name = “Manju” var employee_salary = 400000 var formatted_string = output.format(employee_name, employee_salary) print(formatted_string) }}
Output
输出量
Manju earns 400000 in a month!
Program 2:
程式2:
object MyClass { def main(args: Array[String]) { var name = “Honda CBR 650” var speed = 194 var formatted_string = “%s has a maximum speed of %d Km/h.”.format(name, speed) print(formatted_string) }}
Output
输出量
Honda CBR 650 has a maximum speed of 194 Km/h. formatted()方法 (formatted() method)
The formatted() method works in the same way as the format() method. This method works on objects and it can be used for formatting output that contains text along with integers double values and strings.
formatted()方法的工作方式与format()方法相同 。 此方法适用于对象,可用于格式化包含文本以及整数,双精度值和字符串的输出。
Syntax:
句法:
var formatted_string = value.formatted(unformatted_string)
Programs 1:
程序1:
object MyClass { def main(args: Array[String]) { var speed = 124 var formattedstring = speed.formatted(“Maximum speed of my bike is %d km/h”) print(formattedstring) }}
Output
输出量
Maximum speed of my bike is 124 km/h
Program 2:
程式2:
object MyClass { def main(args: Array[String]) { var bike = “Royal Enfield Thunderbird” var formattedstring = bike.formatted(“I have a %s”) print(formattedstring) }}
Output
输出量
I have a Royal Enfield Thunderbird
翻译自: https://www.includehelp.com/scala/formatting-strings-using-scala-format-and-formatted-methods.aspx
scala 字符串 格式化
84280146