📜  Julia 中的字符串格式

📅  最后修改于: 2021-11-25 04:46:35             🧑  作者: Mango

Julia 是一种由麻省理工学院的人开发的高级语言。它是一种开源的高性能语言。它具有类似于Python的简单语法,但具有 C 的速度和性能。

字符串格式化是使用字符串插值(在已知值之间填充未知值)评估字符串字面量的过程,它产生的结果是占位符被替换为其相应的值。

例如:

word="article"
@printf("This is an %s",word)

上面的代码将用 word 的值替换 ‘%s’,输出将是:

This is an article

大多数当前语言还提供不同类型的格式说明符和不同的格式功能。在 Julia 中,有一些类似于 C 的格式说明符:

  • %c:单个字符(字母、数字、特殊符号)
  • %s:字符串(字符的组合)
  • %d:整数(任何整数(不是分数))
  • %f:浮点数(具有浮动小数点的数字)
  • %.nf:浮点数限制为 n 个小数位
  • %e:浮点数的科学表示

在 Julia 中有两种格式化字符串的方法。

方法 1:使用 Printf 库

Printf 是 Julia 中的一个标准库,它包含在以下语法中:

using Printf

Julia 使用类似于 C 的 printf()函数来格式化字符串。在这里,我们使用宏@printf 进行打印。

打印不同的数据类型

字符:在以下代码中,我们将单个字母 ‘c’ 存储在名为 ‘ 字符’ 的变量中。然后使用 ‘%c’ 格式说明符打印字符变量,因为它的数据类型是 char。为了使说明符功能更易于理解,使用了具有相同数据类型 (字符) 的另一个变量 ‘character2’。

Julia
using printf
character = 'c';
@printf("the letter is %c", character);
character2 = 'a';
@printf("the two letters are %c and %c",
                 character, character2);


Julia
# Loading Library
using Printf
  
# Creating String
s = "GeeksforGeeks";
  
# Formatting first string
@printf("welcome to %s", s);
  
# Creating second string
s2 = "I love";
  
# Formatting both strings
@printf("%s %s", s2, s);


Julia
using Printf
integer = 33;
@printf("the value is %d",integer);
integer2 = 42;
@printf("the sum is %d + %d = %d",
                integer, integer2, 
                integer + integer2);


Julia
using Printf
floatvalue = 0.08;
@printf("the floating point value is %f", 
                             floatvalue);
floatvalue2 = 1.92;
@printf("the value of %f + %f = %d", 
            floatvalue, floatvalue2,
            floatvalue + floatvalue2);


Julia
using Printf
floatvalue = 20.3658;
@printf("the value of float restricted to 2 decimal places %.2f",
                                                     floatvalue);


Julia
using Printf
floatvalue = 20.3658;
@printf("scientific representation of the floatvalue is %e",
                                                floatvalue);


Julia
using Formatting
  
# to print a string
site = "GeeksforGeeks";
printfmt("Welcome to {:s}", site)
  
# to print a single character
c = 'a';
printfmt("Character is {:c}", c)
  
# to print an integer
num = 40;
printfmt("Integer is {:d}", num)
  
# to print a float value
floatnum = 178.33987;
printfmt("Float value is {:f}", floatnum)
  
# to print a float value 
# restricted to 2 decimal places
printfmt("Restricted to 2 decimal places {:.2f}",
                                        floatnum)
  
# to print a float value 
# in scientific representation
printfmt("Scientific representation {:e}",
                                 floatnum)


在输出中,可以清楚地看到,在第一个 printf 语句中,%c 被替换为字符变量的值,而在第二个 printf 语句中,%c 都被替换为字符和 character2 的值。

字符串:在以下示例中,字符串“GeeksforGeeks”存储在变量“s”中,并使用“%s”说明符和 printf 打印。为了使这段代码更易于理解,添加了另一个字符串S2,其值为“I love”,并且将两个字符串打印在一起使其成为“I love GeeksforGeeks”。

朱莉娅

# Loading Library
using Printf
  
# Creating String
s = "GeeksforGeeks";
  
# Formatting first string
@printf("welcome to %s", s);
  
# Creating second string
s2 = "I love";
  
# Formatting both strings
@printf("%s %s", s2, s);

在输出中,您可以看到如何在第一个 printf 语句中将 %s 替换为 s 的值。同样,在第二个 printf 语句中,%s 都被 s2 和 s 替换。

整数:在下面的代码中,我们将一个名为整数的变量定义为 33。然后我们使用 ‘%d’ 说明符打印它。类似地,我们定义了另一个变量 integer2 并将其值设置为 42。在第二个 printf 语句中,我们打印了这两个数字的总和。

朱莉娅

using Printf
integer = 33;
@printf("the value is %d",integer);
integer2 = 42;
@printf("the sum is %d + %d = %d",
                integer, integer2, 
                integer + integer2);

在输出中,查看第一个 printf 语句中如何将 ‘%d’ 替换为整数值。并且,在第二个 printf 语句中,%d、%d 和 %d 被替换为 integer、integer2 和 integer+integer2。

Float:在下面的代码中,我们声明了一个变量“floatvalue”并将其设置为 0.08。然后我们使用 ‘%f’ 说明符打印了这个值。然后我们再初始化一个变量’floatvalue2’并将其设置为1.92。在第二个 printf 语句中,我们使用说明符 ‘%d’ 和 ‘%f’ 打印了这两个值的总和。

朱莉娅

using Printf
floatvalue = 0.08;
@printf("the floating point value is %f", 
                             floatvalue);
floatvalue2 = 1.92;
@printf("the value of %f + %f = %d", 
            floatvalue, floatvalue2,
            floatvalue + floatvalue2);

您可以看到第一个输出中的 %f 是如何被 0.08 替换的,而 %f、%f 和 %d 是如何被分别替换为 0.08、1.92 和 2 的。 (请注意我们如何使用 ‘%d’ 打印此总和并获得总和的整数值,如果您希望获得 sum 的浮点值,您可以使用 ‘%f’ 说明符。)

浮点值限制为 2 位小数:假设我们有 pi 的值并且我们想要显示它,但不是显示它的整个值,我们只想打印值“3.14”,即该值应限制为 2 位小数地方。为此,我们使用 %.nf (n 是限制)。

朱莉娅

using Printf
floatvalue = 20.3658;
@printf("the value of float restricted to 2 decimal places %.2f",
                                                     floatvalue);

在此输出中,您可以看到 20.3658 如何打印为 20.37,因为它四舍五入到小数点后两位。

浮点数的科学表示:您可以使用 ‘%e’ 说明符打印出浮点数的科学表示。它通常用于表示非常大或非常小的数字。

朱莉娅

using Printf
floatvalue = 20.3658;
@printf("scientific representation of the floatvalue is %e",
                                                floatvalue);

在此输出中,请注意 ‘floatvalue’ (20.3658) 如何显示为 2.063580e+01。发生这种情况是因为我们使用了 ‘%e’ 说明符,它打印出了它的科学表示。

方法二:使用Formatting.jl包

Formatting.jl 是一个开源的 Julia 包,旨在为 Julia 提供类似Python 的格式。这个包在 Github Formatting.jl上可用

Installation Command - Pkg.add("Formatting")

我们可以使用 printfmt 或 printfmtln 进行格式化打印。此外,“fmt”和“format”可用于格式化字符串值。

示例:打印不同的数据类型

在这种方法中,格式化的方式与Python的类似。格式说明符放在大括号{} 内,’ %’符号替换为‘:’ (查看下面的代码)。

朱莉娅

using Formatting
  
# to print a string
site = "GeeksforGeeks";
printfmt("Welcome to {:s}", site)
  
# to print a single character
c = 'a';
printfmt("Character is {:c}", c)
  
# to print an integer
num = 40;
printfmt("Integer is {:d}", num)
  
# to print a float value
floatnum = 178.33987;
printfmt("Float value is {:f}", floatnum)
  
# to print a float value 
# restricted to 2 decimal places
printfmt("Restricted to 2 decimal places {:.2f}",
                                        floatnum)
  
# to print a float value 
# in scientific representation
printfmt("Scientific representation {:e}",
                                 floatnum)

输出

在输出中,您可以看到 {:c}、{:s}、{:d}、{:f}、{:e}、{:.2f} 如何被替换为字符、字符串、整数、浮点数-点数、科学表示法和浮点数分别限制为小数点后 2 位。