如何以智能方式创建固定长度空白字符串?(How to create a fixed lenght blank String in a smart way?)

编程入门 行业动态 更新时间:2024-10-28 08:24:39
如何以智能方式创建固定长度空白字符串?(How to create a fixed lenght blank String in a smart way?)

我有以下情况,我不确定我是否以聪明的方式处理它。

所以我必须创建一个包含一些行的文本文件。

每一行都是由一些其他字符串的串联组成的字符串。

其中一些字符串(我必须连接)取自模型对象(定义为模型对象的字段的值)。

其他一些字符串具有固定值(具有固定长度)。

例如(进入创建连接的方法)我可以有这样的东西:

// FINAL BUILDED String: String outputLine; // FIELDS TO BE CONCATENATED: String jobId = " "; String address = myModelObject.getAddress(); String name = myModelObject.getName(); outputLine = jobId + address + name;

因此,正如您在前面的代码片段中看到的那样, jobId字段必须是由8个空白连续字符组成的固定字符串

我的疑问是:它是正确的还是我可以用更聪明的方式创建一个固定的长度字符串(由所有空白字符组成)? 也许使用Char数组 ? 但我不确定这是一个更聪明的解决方案

TNX

I have the following situation and I am not sure if I am handling it in smart way.

So I have to create a textual file that contains some lines.

Each line is a String composed by the concatenation of some other strings.

Some of these string (that I have to concatenate) are taken from a model object (are the values of the fields definied into a model object).

Some other string have fixed value (with fixed lenght).

For example (into the method that create the concatenation) I could have something like this:

// FINAL BUILDED String: String outputLine; // FIELDS TO BE CONCATENATED: String jobId = " "; String address = myModelObject.getAddress(); String name = myModelObject.getName(); outputLine = jobId + address + name;

So, as you can see from the previous snippet, the jobId field have to be a fixed String composed by 8 blanks consecutive characters

My doubt is: is it correct or can I crete a fixed lenght String (composed by all blanks characters) in some smarter way? Maybe using an array of Char? But I am not sure that it is a smarter solution

Tnx

最满意答案

一种常见的方法是使用Arrays.fill 。

public static String makeFilledString(int n, char c) { char[] ca = new char[n]; Arrays.fill(ca, c); return new String(ca); }

不知道这是否比在循环中构建一个更高效或更低效,但至少它允许您配置字段宽度而不是将其长度隐藏在硬编码字符串后面。

请注意,此技术始终会创建一个新String 。 这可能会导致您希望使用更多内存。 在Map跟踪它们是有益的,这样您就不会重复创作。

private static Map<Integer, String> spaces = new HashMap<>(); public static String spaces(int n) { String s = spaces.get(n); if (s == null) { spaces.put(n, s = makeFilledString(n, ' ')); } return s; }

注意:以上内容不是线程安全的。

One common method is to use Arrays.fill.

public static String makeFilledString(int n, char c) { char[] ca = new char[n]; Arrays.fill(ca, c); return new String(ca); }

No idea whether this is more or less efficient than just building one in a loop but at least it allows you to configure your field widths instead of hiding their lengths behind a hard-coded string.

Note that this technique always creates a new String. This may result in more memory being used that you would wish. It can be beneficial to keep track of them in a Map so you don't repeat the creations.

private static Map<Integer, String> spaces = new HashMap<>(); public static String spaces(int n) { String s = spaces.get(n); if (s == null) { spaces.put(n, s = makeFilledString(n, ' ')); } return s; }

NB: The above is not thread-safe.

更多推荐

本文发布于:2023-07-31 09:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1342578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   长度   空白   方式   智能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!