字符串操作 [英] string manip

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

问题描述

我需要一些帮助来编写一个函数将


" name:john"

转换为

"< ;名称> john< / name>"

它应该与不同的标签兼容,而不仅仅是名称


" name:john"

" email:so ******* @ abc.com"

into

"< name> john< / name>

"< email> so ******* @ abc.com< / email>"

提前致谢

Aaron

I need some help writing a function that converts

"name:john"
into
"<name>john</name>"
it should be compatible with different tags, not just names

"name:john"
"email:so*******@abc.com"
into
"<name>john</name>"
"<email>so*******@abc.com</email>"
Thanks in advance
Aaron

推荐答案

这是一个简单的例子;


string sample =" name: john" ;;

string resultString = String.Format("< {0}> {1}< / {0}>",sample.Split('':'' ));


但你应该检查可能导致异常的数组长度......


-


谢谢,

Yunus Emre ALP?ZEN

BSc,MCAD.NET


" ;亚伦" <ク***** @ yahoo.com>在留言中写道

新闻:uD ************** @ tk2msftngp13.phx.gbl ...
Here is a simple example;

string sample = "name:john";
string resultString = String.Format("<{0}>{1}</{0}>",sample.Split('':''));

But u should check array lengths, etc. that may cause an exception....

--

Thanks,
Yunus Emre ALP?ZEN
BSc, MCAD.NET

"Aaron" <ku*****@yahoo.com> wrote in message
news:uD**************@tk2msftngp13.phx.gbl...
我需要一些帮助写作将

" name:john"
转换为
"< name> john< / name>"

的函数兼容不同的标签,而不仅仅是名称

" name:john"
" email:so ******* @ abc.com"
into < br&>"< name> john< / name>"
"< email> so ******* @ abc.com< / email>"

提前致谢
Aaron
I need some help writing a function that converts

"name:john"
into
"<name>john</name>"
it should be compatible with different tags, not just names

"name:john"
"email:so*******@abc.com"
into
"<name>john</name>"
"<email>so*******@abc.com</email>"
Thanks in advance
Aaron



在消息< uD ************** @ tk2msftngp13 .phx.gbl>,Aaron

< ku ***** @ yahoo.com>写道
In message <uD**************@tk2msftngp13.phx.gbl>, Aaron
<ku*****@yahoo.com> writes
我需要一些帮助来编写一个将

name:john
转换成
"< name> john< / name>"
I need some help writing a function that converts

"name:john"
into
"<name>john</name>"




如果练习的目的是构建格式良好的XML,并且如果你要构建任何构建XML的b $ b文档的大小或复杂性,值得思考

关于使用XMLDocument来做到这一点。它会比使用stringbuilder的字符串更慢,但是它会确保xlm是良好的形成,字符是否正确转义等等。


如果它只是字面意思你上面写的(看起来很可疑

就像家庭作业),看看使用string的split方法,其中

根据您选择的

分隔符将字符串分成较短字符串数组。然后你可以使用+连接数组中的字符串

来得到你想要的任何东西。


-

Steve Walker



If the point of the exercise is to build well-formed XML, and if you are
going to build any size or complexity of document, it''s worth thinking
about using an XMLDocument to do it. It will be slower than string
concatenation with a stringbuilder, but it will ensure that the xlm is
well formed, characters are escaped correctly, etc.

If it''s just literally what you wrote above (which looks suspiciously
like homework), look at the using the split method of string, which
breaks a string into an array of shorter strings based on whatever
delimiter you choose. You can then concatenate the strings in the array
using + to get whatever you want.

--
Steve Walker


我用什么方法遍历每一行?


pesudo代码


string ToXml(字符串输入)

{

string resultString ="" ;;


输入中的每一行

{

resultString = String.Format("< {0}> {1}< / {0}>",input.Split(' ':''));

}

返回resultString;

}


这是我需要做什么

函数查看一行,找到之前的:然后用开启和关闭的xml标签包围

行。


Yunus Emre ALP?ZEN [MCAD.NET]" <叶*** @ msakademik.net>在消息中写道

新闻:uw ************** @ TK2MSFTNGP12.phx.gbl ...
what method do i use to loop through each line?

pesudo code

string ToXml(string input)
{
string resultString = "";

for each line in input
{
resultString = String.Format("<{0}>{1}</{0}>",input.Split('':''));
}
return resultString ;
}

this is what i need to do
the function looks at a line, find whats before the : and then surrounds the
line with the opening and closing xml tags.

"Yunus Emre ALP?ZEN [MCAD.NET]" <ye***@msakademik.net> wrote in message
news:uw**************@TK2MSFTNGP12.phx.gbl...
这是一个简单的例子;

string sample =" name:john";
string resultString = String.Format("< {0}> {1}< / {0}> ",sample.Split('':''));

但你应检查可能导致异常的数组长度......

- -

谢谢,
Yunus Emre ALP?ZEN
BSc,MCAD.NET

Aaron <ク***** @ yahoo.com>在消息中写道
新闻:uD ************** @ tk2msftngp13.phx.gbl ...
Here is a simple example;

string sample = "name:john";
string resultString = String.Format("<{0}>{1}</{0}>",sample.Split('':''));

But u should check array lengths, etc. that may cause an exception....

--

Thanks,
Yunus Emre ALP?ZEN
BSc, MCAD.NET

"Aaron" <ku*****@yahoo.com> wrote in message
news:uD**************@tk2msftngp13.phx.gbl...
我需要一些帮助来编写一个转换函数

" name:john"
进入
"< name> john< / name>"

它应与不同的标签,而不仅仅是名称

" name:john"
" email:so ******* @ abc.com"
into
" ;< name> john< / name>"
"< email> so ******* @ abc.com< / email>"

谢谢推进
Aaron
I need some help writing a function that converts

"name:john"
into
"<name>john</name>"
it should be compatible with different tags, not just names

"name:john"
"email:so*******@abc.com"
into
"<name>john</name>"
"<email>so*******@abc.com</email>"
Thanks in advance
Aaron




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

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