在java中格式化字符串 [英] Formatting strings in java

查看:124
本文介绍了在java中格式化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用readLine()方法读取串行缓冲区。 readLine()返回的字符串格式为str1:str2。在while循环中,当我使用readLine()响应串口命令的次数时,我得到奇怪的输出:

I am reading the serial buffer with readLine() method. The string returned by readLine() is in the format "str1 : str2". In a while loop when I use readLine() number of times for a response of serial port command I get weird output like this :

String1 : string1
SString11 : String2
StringString2 : String23 
String4 : String5

但我需要格式化如下的输出

But I need the output formatted as below

String1       : string1
SString11     : String2
StringString2 : String23 
String4       : String5

我在字符串上使用了split方法并得到了两个用分隔符分隔的字符串为':'。但是现在我需要在String1上添加空格以对齐所有冒号。

I used the split method on string and get the two strings separated with delimiter as ':'. But now I need to append the String1 with spaces to align all the colons.

对不起如果我的问题解释很奇怪。但如果有人理解这个问题你能建议怎么做吗?

I am sorry If my problem explanation is weird. But If anybody understood the problem can you please suggest how to do it?

推荐答案

看看 System.out.printf

这篇关于在java中格式化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆