Java:在动态索引位置插入String,其中索引是格式化字符串 [英] Java: Insert a String at a dynamic index position where the index is a formatted string

查看:107
本文介绍了Java:在动态索引位置插入String,其中索引是格式化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字符串格式如下:

{a},{b2},{str},{5}...

{a} - index[0], {b2} - index[1],(str} - index[2],{5} - index[3] ...

这可能超过20K或25K索引。即使它上升到1L。

this could be more than 20K or 25K indexes. Even it goes up to 1L.

我想在任何位置插入字符串{c3},它是动态的(对于前3)
结果应该是这样的

I want to insert the string "{c3}" at any position, its a dynamic (for ex 3rd) the result should be like this

{a},{b2},{str},{c3},{5}

这是一个非常频繁的操作,不是在控制台应用程序中使用。它适用于面向Web的应用程序。那么在java中处理这个的更好的方法是什么?

It's a very frequent operation, not using in console application. It's for a web oriented application. So which is the better way to handle this in java?

使用split方法我们必须使用System.arraycopy来处理它。对于性能方面,它有点困难。

Using split method we have to use System.arraycopy to handle it. For the performance side it's a little bit difficult.

所以请建议我如何处理这个是在REGEX。还指导我是否是处理它的好方法或告诉我是否有更好的方法?

So pls suggest me how to handle this is in REGEX. Also guide me whether it is the good way to handle it or tell me if there is a better way?

谢谢你的快速r eply。

Thanks for ur quick reply.

是的,这是解决我问题的方法之一。

Yes this is one of the way to solve my problem.

我会清楚地解释我的问题。

I will explain my problem clearly.

我从文件库中读取文件内容,最后得到如下内容:{a},{b2},{str},{5} ...

I read the file content from the filestore and finally get the content like this:{a},{b2},{str},{5}...

在该字符串中,我必须在特定索引处插入一些内容(索引基于{}),之后必须将其存储到文件存储中。

In that string i have to insert some content at particular index(index is based on {}), after that have to stored it into filestore.

在我的情况下,读写不是问题。我们会处理它。

Read and writing is not problem in my case.we will handle it.

插入particluar索引只是问题。服务器是否能够在频繁点击时服务良好

Inserting the particluar index is only problem. whether the server is serve well or not when it is going to hit frequently

更多疑问:

假设该字符串具有超过25K的索引,在这种情况下,while循环执行25k次。

Suppose the string is having more than 25K indexes, in that case the while loop is execute 25k times.

插入是频繁的操作,它可以被击中超过50次分钟。(50 * 25 = 1250K)

Insert is the frequent operation,it can be hit morethan 50 times in a minute.(50*25=1250K)

假设25个用户进入同一台服务器是否可以很好地服务?

Suppose 25users comes into the same server in that case whether it can be serve well?

我不知道哪种方式更好。你的模特是否适合我的情况?

I dont know which way is better. Whether your model is suitable for my case?

请告诉我......

Please advise me...

推荐答案

正则表达式实际上可能比根据需要显式解析更慢。所需的表达式也可能有点复杂。这是一个不需要大数组的替代方案:

It's possible that regex is actually slower than parsing explicitly for what you need. The expression required might be a bit complex too. Here's an alternative to consider that doesn't require a large array:

String input = "{a},{b2},{str},{5}";
int insertPos = 3;
String insertText = "{c3}";

StringBuilder sb = new StringBuilder();
int pos = 0;
Scanner scanner = new Scanner(input).useDelimiter(",");
while (scanner.hasNext()) {
    String next = scanner.next();
    if (pos++ == insertPos) {
        sb.append(insertText);
        sb.append(',');
    }
    sb.append(next);
    sb.append(',');
}
sb.deleteCharAt(sb.length() - 1); // remove the last comma
String output = sb.toString(); // output will be "{a},{b2},{str},{c3},{5}"

我不确定你的用例是什么,但是你可以根据需要调整它来处理更多的插入。

I'm not sure what your use case is, however you can adapt this to handle more inserts as necessary.

编辑:这是一个快50倍的版本。给定具有50K索引的字符串,它可以在典型硬件上的单个线程中每分钟处理大约100K输入字符串(所以50亿索引/分钟)。

Here's a version that's 50 times faster. Given strings with 50K indexes each it can process roughly 100K input strings per minute in a single thread on typical hardware (so 5 billion indexes/minute).

String input = "{a},{b2},{str},{5}";
int insertPos = 3;
String insertText = "{c3}";

String output = input + ","; // add temporary comma
int index = 0;
int pos = 0;
while (index > -1) {
    if (pos == insertPos) {
        output = output.substring(0, index)
                + (pos == 0 ? insertText + "," : "," + insertText)
                + output.substring(index);
    }
    index = output.indexOf(',', index + 1);
    pos++;
}
output = output.substring(0, output.length() - 1); // remove last comma
System.out.println(output); // output will be "{a},{b2},{str},{c3},{5}"

这篇关于Java:在动态索引位置插入String,其中索引是格式化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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