关于如何构建长字符串的建议 [英] Recommendation on How to build a long string

查看:74
本文介绍了关于如何构建长字符串的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试编写一个代码来构建一个长达768个字符的字符串。

这个字符串将被写入一个文件然后由另一个应用程序读取
。字符串的格式已经定义。对于

示例Firstname从位置20开始,Last Name在30位开始,

位置在40位等...我正在从sql读取这些数据

数据源并且必须替换确切位置的值

确保paramteres完全位于他们需要的位置。我现在正在运行
,但我正在寻找一种更智能的方法。我是

试图构建一个包含768个空格的字符串来进行初始化。然后用一个字符替换

空格,以确保保留所有

参数的位置。我尝试使用stringbuilder,但它没有

具有我值得关注的功能。有没有一个聪明的方式

of dong这个?


角色位置

01234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789

FirstName姓氏地址

street City st Zip xxx xxxxx xxxx


谢谢,

Mo

解决方案

Mo< le ****** @ yahoo.comwrote:


我是试图编写一个代码来构建一个768个字符长的字符串。

这个字符串将被写入一个文件,然后由另一个应用程序读取
。字符串的格式已经定义。对于

示例Firstname从位置20开始,Last Name在30位开始,

位置在40位等...我正在从sql读取这些数据

数据源并且必须替换确切位置的值

确保paramteres完全位于他们需要的位置。我现在正在运行
,但我正在寻找一种更智能的方法。我是

试图构建一个包含768个空格的字符串来进行初始化。然后用一个字符替换

空格,以确保保留所有

参数的位置。我尝试使用stringbuilder,但它没有

具有我值得关注的功能。任何身体都有聪明的方式

of dong这个?



首先,你确定你真的需要加快速度吗?您的当前代码是否可读,并且您是否测量过它的性能?我会

只有在遇到问题时才开始寻找更复杂的选项。


话虽如此,你可以创建一个存储类型的类型char数组和

允许你轻松地替换部分 - 然后适当地从该char数组创建一个新的字符串




-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复群组,请不要给我发邮件


"沫" < le ****** @ yahoo.com在留言中写道

新闻:11 ********************** @ y80g2000hsf .googlegr oups.com ...


是否有任何机构有这种智能方式吗?



你试过这个:?
http://msdn.microsoft.com/library/de...ighttopic1.asp


3月25日下午1点04分,Mo < le_mo ... @ yahoo.comwrote:





我正在尝试编写代码来构建一个字符串长768个字符。

这个字符串将被写入一个文件,然后由另一个应用程序读取
。字符串的格式已经定义。对于

示例Firstname从位置20开始,Last Name在30位开始,

位置在40位等...我正在从sql读取这些数据

数据源并且必须替换确切位置的值

确保paramteres完全位于他们需要的位置。我现在正在运行
,但我正在寻找一种更智能的方法。我是

试图构建一个包含768个空格的字符串来进行初始化。然后用一个字符替换

空格,以确保保留所有

参数的位置。我尝试使用stringbuilder,但它没有

具有我值得关注的功能。有没有一个聪明的方式

这个?


字符位置

01234567890123456789012345678901234567890123456789 0123456789012345678901234 * 567890123456789

FirstName姓氏地址

street City st Zip xxx xxxxx xxxx



我会这样做:


if(firstName.Length 10)firstName = firstName.Substring(0,10);

.... etc ...

longString = String.Format(" {0,10} {1,10} ...",firstName,

lastName,...);


这是最容易阅读的内容。我只是在

的情况下调整它的速度,我要对它进行基准测试并发现它在我的代码中确实是一个

的瓶颈。


Hi,

I am trying to write a code to build a string 768 characters long.
This string is going to be written to a file which is then read by
another application. The format of the string is already defined. For
example Firstname starts at position 20, Last Name at position 30,
address at position 40 etc... I am reading these data from sql
datasource and must substitute the values in the exact position
insuring that paramteres end up exactly at position they need to be. I
have this running now but I am looking for a smarter method. I am
trying to build a string with 768 spaces to initalize. Then replace
the spaces with characters one at a time to insure location of all
parameters are preserved. I tried using stringbuilder but it does not
have the functionality I am lookign for. Does Any body has a smart way
of dong this?

character Position
01234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789
FirstName LastName Address
street City st Zip xxx xxxxx xxxx

Thanks,
Mo

解决方案

Mo <le******@yahoo.comwrote:

I am trying to write a code to build a string 768 characters long.
This string is going to be written to a file which is then read by
another application. The format of the string is already defined. For
example Firstname starts at position 20, Last Name at position 30,
address at position 40 etc... I am reading these data from sql
datasource and must substitute the values in the exact position
insuring that paramteres end up exactly at position they need to be. I
have this running now but I am looking for a smarter method. I am
trying to build a string with 768 spaces to initalize. Then replace
the spaces with characters one at a time to insure location of all
parameters are preserved. I tried using stringbuilder but it does not
have the functionality I am lookign for. Does Any body has a smart way
of dong this?

Firstly, are you sure you actually need to make this faster? Is your
current code readable, and have you measured its performance? I would
only start going for more complicated options when you''ve got an issue.

Having said that, you could create a type which stores a char array and
allows you to replace portions of it easily - then create a new string
from that char array appropriately.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


"Mo" <le******@yahoo.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...

Does Any body has a smart way of dong this?

Have you tried this:?
http://msdn.microsoft.com/library/de...ighttopic1.asp


On Mar 25, 1:04 pm, "Mo" <le_mo...@yahoo.comwrote:

Hi,

I am trying to write a code to build a string 768 characters long.
This string is going to be written to a file which is then read by
another application. The format of the string is already defined. For
example Firstname starts at position 20, Last Name at position 30,
address at position 40 etc... I am reading these data from sql
datasource and must substitute the values in the exact position
insuring that paramteres end up exactly at position they need to be. I
have this running now but I am looking for a smarter method. I am
trying to build a string with 768 spaces to initalize. Then replace
the spaces with characters one at a time to insure location of all
parameters are preserved. I tried using stringbuilder but it does not
have the functionality I am lookign for. Does Any body has a smart way
of dong this?

character Position
01234567890123456789012345678901234567890123456789 0123456789012345678901234*567890123456789
FirstName LastName Address
street City st Zip xxx xxxxx xxxx

I would do something like this:

if (firstName.Length 10) firstName = firstName.Substring(0, 10);
....etc...
longString = String.Format("{0,10}{1,10}...", firstName,
lastName, ... );

This is the easiest to read. I would tune it for speed only in the
case that I were to benchmark it and find that it really is a
bottleneck in my code.


这篇关于关于如何构建长字符串的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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