📜  C#| PadRight()方法

📅  最后修改于: 2021-05-29 16:21:03             🧑  作者: Mango

在C#中, PadRight()是一个字符串方法。这种方法是通过用填充空格他们用于左对齐在字符串中的字符或右侧指定字符,对于一个指定的总长度。可以通过向其传递不同的参数来重载此方法。

  • String.PadRight方法(Int32)
  • String.PadRight方法(Int32,Char)

String.PadRight方法(Int32)

此方法用于通过在右边的空格处填充字符来使该字符串的字符左对齐。参数“ totalWidth ”将指定字符串的填充字符数,此方法将返回一个新字符串。

句法:

public string PadRight(int totalWidth)
  • 参数:此方法将接受一个参数“ totalWidth ”,该参数指定字符串的填充字符数。此参数的类型为System.Int32
  • 返回值:该方法将返回字符串左侧部分的字符串。返回值类型为System.String

异常:如果totalWidth小于零,则将出现ArgumentOutOfRangeException

例子:

Input : str  = "GeeksForGeeks"
        str.PadRight(2);
Output: 'GeeksForGeeks' 

// String is same because totalWidth 
// is less than length of String.

Input : str  = "GeeksForGeeks"
        str.PadRight(13);
Output: 'GeeksForGeeks' 

// String is same because of totalWidth 
// is equal to the length of String.

Input : str  = "GeeksForGeeks"
        str.PadRight(20);
Output: 'GeeksForGeeks       '   

// String is changed because of totalWidth
// is greater than the length of String.

So Right Padding will show only if the 
totalWidth is greater than string length.

下面的程序说明了上面讨论的方法:

// C# program to illustrate the
// String.PadRight(totalWidth) method
using System;
class Geeks {
  
    // Main Method
    public static void Main()
    {
        string s1 = "GeeksForGeeks";
  
        Console.WriteLine("String : " + s1);
  
        // totalwidth is less than string length
        Console.WriteLine("Pad 2 :'{0}'", s1.PadRight(2));
  
        // totalwidth is equal to string length
        Console.WriteLine("Pad 13 :'{0}'", s1.PadRight(13));
  
        // totalwidth is greater then string length
        Console.WriteLine("Pad 20 :'{0}'", s1.PadRight(20));
    }
}
输出:
String : GeeksForGeeks
Pad 2 :'GeeksForGeeks'
Pad 13 :'GeeksForGeeks'
Pad 20 :'GeeksForGeeks       '

String.PadRight方法(Int32,字符)

此方法用于通过在右边的指定字符填充字符来使该字符串的字符左对齐。参数“ totalWidth ”将指定字符串的填充字符数,“ paddingChar”是指定的字符 。

句法:

public string PadRight(int totalWidth, char paddingChar)
  • 参数:该方法接受两个参数“ totalWidth ”和“ paddingChar ”。参数“ totalWidth”将指定字符串的填充字符数,并且此参数的类型为System.Int32 。参数“ paddingChar”将指定填充字符,并且此参数的类型为System.Char
  • 返回值:该方法将返回一个新字符串,该字符串将等效于当前字符串,但是将其左对齐并在右侧填充“ paddingChar”参数指定的字符。如果totalWidth小于字符串的长度,则该方法返回相同的字符串。如果totalWidth等于字符串的长度,则该方法返回一个与当前String相同的新字符串。返回值类型为System.String

异常:如果totalWidth小于零,则将出现ArgumentOutOfRangeException

例子:

Input : str  = "GeeksForGeeks"
        str.PadRight(2, '*');
Output: 'GeeksForGeeks' 

// String is same because totalWidth
// is less than the length of String.

Input : str  = "GeeksForGeeks"
        str.PadRight(13, '*');
Output: 'GeeksForGeeks' 

// String is same because of totalWidth
// is equal to the length of String.

Input : str  = "GeeksForGeeks"
        str.PadRight(20, '*');
Output: 'GeeksForGeeks*******'   

// String is changed because totalWidth 
// is greater than the length of String.

下面的程序说明了上面讨论的方法:

// C# program to illustrate the
// String.PadRight(int totalWidth, 
// char paddingChar) method 
using System;
class Geeks {
  
    // Main Method
    public static void Main()
    {
        string s1 = "GeeksForGeeks";
        char pad = '*';
        Console.WriteLine("String : " + s1);
  
        // totalwidth is less than string length
        Console.WriteLine("Pad 2 :'{0}'", s1.PadRight(2, pad));
  
        // totalwidth is equal to string length
        Console.WriteLine("Pad 13 :'{0}'", s1.PadRight(13, pad));
  
        // totalwidth is greater then string length
        Console.WriteLine("Pad 20 :'{0}'", s1.PadRight(20, pad));
    }
}
输出:
String : GeeksForGeeks
Pad 2 :'GeeksForGeeks'
Pad 13 :'GeeksForGeeks'
Pad 20 :'GeeksForGeeks*******'

参考:

  • https://msdn.microsoft.com/zh-CN/library/system。字符串.padright1
  • https://msdn.microsoft.com/zh-CN/library/system。字符串.padright2