Skip to main content
 首页 » 编程设计

c#之使用动态给定的长度在 C# 中左对齐一个字符串

2024年02月23日28rubylouvre

我正在尝试编写一个左对齐函数,例如:

private static string LeftJustify(string field, int len) 
{ 
    string retVal = string.empty; 
 
    ///todo: 
 
   return retVal; 
 
} 

你能帮我把逻辑放在函数中吗?

请您参考如下方法:

如果你只是想填充字符串,你可以使用 String.PadRight直接:

private static string LeftJustify(string field, int len) 
{ 
    return field.PadRight(len); 
}