一代填充空格的固定长度字符串

我需要创建一个固定的长度字符串,以基于符号的位置生成文件。 丢失的字符必须填充空格。

例如,该字段 CITY 它具有固定长度 15 符号。 对于输入 "Chicago" 和 "Rio de Janeiro" 输出
" Chicago"
" Rio de Janeiro"
.
已邀请:

詹大官人

赞同来自:

以。。。开始 Java 1.5 我们可以使用该方法
http://docs.oracle.com/javase/ ... ocale,%20java.lang.String,%20java.lang.Object...%29
和使用 printf 作为一种格式。

格式行
"%1$15s"

执行这项工作。 在哪里
1$

表示参数索引,
s

表示该参数是一个字符串,而且
15

表示线的最小宽度。
将所有这些折叠在一起:
"%1$15s"

.

对于一般方法,我们有:


public static String fixedLengthString/String string, int length/ {
return String.format/"%1$"+length+ "s", string/;
}


也许有人可以提供另一种格式,以填充具有特定符号的空的地方?

喜特乐

赞同来自:

使用填充
String.format

空间并用所需的符号替换它们。


String toPad = "Apple";
String padded = String.format/"%8s", toPad/.replace/' ', '0'/;
System.out.println/padded/;


打印
000Apple

.

更新

更富有成效的版本 /因为她不依赖
String.format

/, 谁没有空间问题 /感谢Raphael Borhe的提示/.


int width = 10;
char fill = '0';

String toPad = "New York";
String padded = new String/new char[width - toPad.length//]/.replace/'\0', fill/ + toPad;
System.out.println/padded/;


打印
00New York

.

但是您需要添加一个检查,以防止尝试创建具有负长度的字符数组。

龙天

赞同来自:

此代码将具有精确指定的字符数; 装满空格或右侧截断:


private String leftpad/String text, int length/ {
return String.format/"%" + length + "." + length + "s", text/;
}

private String rightpad/String text, int length/ {
return String.format/"%-" + length + "." + length + "s", text/;
}

小姐请别说爱

赞同来自:

您还可以编写一个简单的方法,如下所示。


public static String padString/String str, int leng/ {
for /int i = str.length//; i <= leng; i++/
str += " ";
return str;
}

奔跑吧少年

赞同来自:

对于您需要的右垫
String.format/"%0$-15s", str/


即。标志
-

会有一个地毯 "right" 没有迹象
-

会有一个地毯 "left"

在这里看到我的例子。

http://pastebin.com/w6Z5QhnJ
输入数据应该是字符串和数字

输入的示例: Google 1

卫东

赞同来自:


https://github.com/google/guava/wiki

https://google.github.io/guava ... char-
, 究竟是您想要的,以及许多其他有用的实用程序。

二哥

赞同来自:

import org.apache.commons.lang3.StringUtils;

String stringToPad = "10";
int maxPadLength = 10;
String paddingCharacter = " ";

StringUtils.leftPad/stringToPad, maxPadLength, paddingCharacter/


比番石榴太多好。 从未见过单一的企业项目 Java 它使用番石榴,但是 Apache 行公用事业是最常见的事情。

奔跑吧少年

赞同来自:

这是一个聪明的技巧:


// E.g pad/"sss","00000000"/; should deliver "00000sss".
public static String pad/String string, String pad/ {
/*
* Add the pad to the left of string then take as many characters from the right
* that is the same length as the pad.
* This would normally mean starting my substring at
* pad.length// + string.length// - pad.length// but obviously the pad.length//'s
* cancel.
*
* 00000000sss
* ^ ----- Cut before this character - pos = 8 + 3 - 8 = 3
*/
return /pad + string/.substring/string.length///;
}

public static void main/String[] args/ throws InterruptedException {
try {
System.out.println/"Pad 'Hello' with ' ' produces: '"+pad/"Hello"," "/+"'"/;
// Prints: Pad 'Hello' with ' ' produces: ' Hello'
} catch /Exception e/ {
e.printStackTrace//;
}
}

诸葛浮云

赞同来自:

以下是测试用例的代码 ;/ :


@Test
public void testNullStringShouldReturnStringWithSpaces// throws Exception {
String fixedString = writeAtFixedLength/null, 5/;
assertEquals/fixedString, " "/;
}

@Test
public void testEmptyStringReturnStringWithSpaces// throws Exception {
String fixedString = writeAtFixedLength/"", 5/;
assertEquals/fixedString, " "/;
}

@Test
public void testShortString_ReturnSameStringPlusSpaces// throws Exception {
String fixedString = writeAtFixedLength/"aa", 5/;
assertEquals/fixedString, "aa "/;
}

@Test
public void testLongStringShouldBeCut// throws Exception {
String fixedString = writeAtFixedLength/"aaaaaaaaaa", 5/;
assertEquals/fixedString, "aaaaa"/;
}


private String writeAtFixedLength/String pString, int lenght/ {
if /pString != null && !pString.isEmpty///{
return getStringAtFixedLength/pString, lenght/;
}else{
return completeWithWhiteSpaces/"", lenght/;
}
}

private String getStringAtFixedLength/String pString, int lenght/ {
if/lenght < pString.length///{
return pString.substring/0, lenght/;
}else{
return completeWithWhiteSpaces/pString, lenght - pString.length///;
}
}

private String completeWithWhiteSpaces/String pString, int lenght/ {
for /int i=0; i<lenght; +=" " ;="" <="" code]="" div="" i++="" pstring="" pstring;="" return="" tdd="" }[="" 我喜欢="">
<div class="answer_text">

[code]String.format/"s",s/ // pads right
String.format/"%-15s",s/ // pads left


优秀摘要
https://www.journaldev.com/178 ... ample
</div>
<div class="answer_text">
此代码正常工作。
https://i.stack.imgur.com/u9lmy.png

String ItemNameSpacing = new String/new char[10 - masterPojos.get/i/.getName//.length//]/.replace/'\0', ' '/;
printData += masterPojos.get/i/.getName//+ "" + ItemNameSpacing + ": " + masterPojos.get/i/.getItemQty// +" "+ masterPojos.get/i/.getItemMeasure// + "\n";


快乐编码!!
</div>
<div class="answer_text">

public static String padString/String word, int length/ {
String newWord = word;
for/int count = word.length//; count &lt; length; count++/ {
newWord = " " + newWord;
}
return newWord;
}


</div>
<div class="answer_text">
这个简单的功能适用于我:


public static String leftPad/String string, int length, String pad/ {
return pad.repeat/length - string.length/// + string;
}


称呼:


String s = leftPad/myString, 10, "0"/;


</div>
</lenght;>

江南孤鹜

赞同来自:

String.format/"s",s/ // pads right
String.format/"%-15s",s/ // pads left


优秀摘要
https://www.journaldev.com/178 ... ample

董宝中

赞同来自:

此代码正常工作。
https://i.stack.imgur.com/u9lmy.png

String ItemNameSpacing = new String/new char[10 - masterPojos.get/i/.getName//.length//]/.replace/'\0', ' '/;
printData += masterPojos.get/i/.getName//+ "" + ItemNameSpacing + ": " + masterPojos.get/i/.getItemQty// +" "+ masterPojos.get/i/.getItemMeasure// + "\n";


快乐编码!!

诸葛浮云

赞同来自:

public static String padString/String word, int length/ {
String newWord = word;
for/int count = word.length//; count < length; count++/ {
newWord = " " + newWord;
}
return newWord;
}

莫问

赞同来自:

这个简单的功能适用于我:


public static String leftPad/String string, int length, String pad/ {
return pad.repeat/length - string.length/// + string;
}


称呼:


String s = leftPad/myString, 10, "0"/;

要回复问题请先登录注册