C# 字符串PadLeft

PHPz
发布: 2024-09-03 15:16:59
原创
295 人浏览过

填充只不过是在字符串的开头或结尾插入空格或任何 Unicode 字符。在字符串开头插入空格或 Unicode 字符称为从左侧填充字符串。 C# 提供了一个名为 PadLeft() 的方法,可以用来实现此目的。

String 类包含两种重载形式的 PadLeft() 方法:

  • String.PadLeft(Int32, Char)
  • String.PadLeft(Int32)

由于字符串本质上是不可变的,PadLeft() 方法在向其左侧添加指定字符后返回一个新字符串,而不是向现有字符串添加字符。

语法:

String.PadLeft() 方法的两种重载形式的语法如下:

public string PadLeft(int totalLength, char paddingChar);
登录后复制

说明:

上述语法中的 PadLeft() 方法采用两个参数;第一个是一个整数,指定在原始字符串左侧添加指定字符后此方法将返回的字符串的长度。第二个参数用于指定用于填充的 Unicode 字符。

public string PadLeft(int totalLength);
登录后复制

说明:

上述语法中的 PadLeft() 方法只接受一个参数,该参数是一个整数,用于指定在原始字符串左侧添加空格后结果字符串的长度。上述两种重载形式的 PadLeft() 方法都返回一个字符串值,其长度将等于参数中指定的长度。

C# 中 String PadLeft() 方法如何工作?

在 C# 中,“System”命名空间包含一个名为 String 的类,该类用于处理字符串操作,并提供一系列方法来对字符串执行不同的操作。其中一种方法是 String.PadLeft() 方法。该方法用于在字符串的开头(即左侧)添加指定的字符,然后返回指定长度的新字符串。

因此,String.PadLeft() 方法将字符串向右移动。

示例:

string str = "Hello";
string resultedString = str.PadLeft(8, '@');
登录后复制

现在让我们尝试借助上面的示例来理解左填充的概念。在这里,我们通过将结果字符串的总长度传递为“8”和一个填充字符(即“@”)来对字符串(str)应用左填充。这里,原始字符串(即“Hello”)的长度为 5,而我们需要结果字符串的长度为 8。因此,结果字符串的左侧将添加三个“@”字符,从而形成总长度(结果字符串的长度)。原始字符串加上结果字符串中的一些填充字符)等于使用其整数参数传递给方法的长度。

如果用户未在方法中指定任何 Unicode 填充字符,则默认情况下将在原始字符串的左侧添加空格,其方式与添加 Unicode 填充字符(如果指定)相同。现在,如果用户指定结果字符串的总长度小于原始字符串的长度,则该方法将返回对现有实例的引用。

同样,如果用户指定结果字符串的总长度等于原始字符串的长度,则该方法将返回一个与现有字符串相同的新字符串。由于字符串本质上是不可变的,因此 PadLeft() 方法的两种重载形式都会在将指定字符填充到原始字符串的左侧后返回一个新字符串。如果指定的总长度小于零,String.PadLeft() 方法将返回 ArgumentOutOfRangeException。

实现 C# 字符串 PadLeft 方法的示例

以下是 C# 字符串 PadLeft 方法的示例:

示例#1

显示 PadLeft() 方法基本功能的示例。

代码:

using System;
using System.Text;
namespace ConsoleApp4
{
public class Program
{
public static void Main(string[] args)
{
string str = "Hello World!";
try
{
//making the length of the string 15
//by adding 3 '@' characters at the beginning of the string
string resultedStringWithChar = str.PadLeft(15, '@');
Console.WriteLine(resultedStringWithChar);
//making the length of the string 15
//by adding 3 white spaces at the beginning of the string
string resultedStringWithoutChar = str.PadLeft(15);
Console.WriteLine(resultedStringWithoutChar);
Console.ReadLine();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
登录后复制

输出:

C# 字符串PadLeft

我们可以在输出中看到,为了使结果字符串的长度达到 15,结果字符串中添加了三个“@”字符。同样,当我们第二次使用 String.PadLeft() 方法时,我们没有指定任何字符。因此,结果字符串中添加了三个空格。

示例#2

示例显示所需结果字符串的总长度小于或等于原始字符串的长度且总长度小于零的情况。

代码:

using System;
using System.Text;
namespace ConsoleApp4
{
public class Program
{
public static void Main(string[] args)
{
string str = "Hello World!";
try
{
//providing total length as 12
//which is equal to the length of the original string
string resultedStringWithChar = str.PadLeft(12, '@');
Console.WriteLine(resultedStringWithChar);
//providing total length as 10
//which is less than the length of the original string
string resultedStringWithoutChar = str.PadLeft(10);
Console.WriteLine(resultedStringWithoutChar);
resultedStringWithoutChar = str.PadLeft(-1);
Console.WriteLine(resultedStringWithoutChar);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
}
登录后复制

输出:

C# 字符串PadLeft

示例#3

对数组中的多个字符串应用 PadLeft() 方法的示例。

代码:

using System;
using System.Text;
namespace ConsoleApp4
{
public class Program
{
public static void Main(string[] args)
{
string[] strArray = { "Lily", "Rose", "Jasmine", "Sunflower" };
char paddingChar = '#';
try
{
//accessing each string of the array
//using 'foreach' loop
foreach (string str in strArray)
{
//adding '#' at the start of each string
Console.WriteLine(str.PadLeft(10, paddingChar));
//using PadLeft() method without specifying
//any padding character
Console.WriteLine(str.PadLeft(10));
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
}
登录后复制

输出:

C# 字符串PadLeft

结论

在C#中,String.PadLeft()方法用于在字符串的开头或左侧添加指定的字符或空格,以达到所需的字符串长度。该方法存在于“System”命名空间下,并且有两种重载形式。

以上是C# 字符串PadLeft的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!