明智地分割字符串特定字符 [英] split a string specific character wise

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

问题描述

亲爱的先生,



我的字符串就像

string teststr =* 123367676789990098877665655444444343445445555555555555555;

i希望将带有第一个字符(*)的字符串拆分为3个下一个字符,直到最后一个。

输出如:



* 123

* 367

* 676

* 789

* 990

* 098

* 877

* 665

* 655

* 444

* 444

* 343

* 445

* 445

* 555

* 555

* 555

* 555

* 555



plz帮帮我。

解决方案

有几种方法可以做 - 你可以使用一个循环和string.Substring方法,或者(可能)一个正则表达式 - 但是最后一个可能需要一个循环或Linq方法来重建带有*前缀的字符串。

循环选项非常简单:

使用string.IndexOf查找*字符。

使用for循环,从星号后的字符开始,以字符串长度结束,每次递增3。

在循环中,使用string.Substring提取这三个字符,并将它们与*前缀一起添加到字符串列表中。


 XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.Load(@D:\Prod.xml);

XmlNode node = xmlDoc.DocumentElement.FirstChild;
XmlNodeList lstVideos = node.ChildNodes;
for(int i = 0; i < lstVideos.Count; i ++)

{



for (int j = 0; j < < span class =code-attribute> 3; j ++)

< span class =code-attribute> {

Response.Write( + lstVideos [j] .InnerText + );

< span class =code-attribute> }

Response.Write(< br > );
}













xml文件是:

 <?  xml     version   =  1.0  >  
- < DocumentElement >
- < test >
< c0 > manas < / c0 >
< c1 > 100 < / c1 >
< c2 > 20 < / c2 >
< c3 > 33 < / c3 >
< c4 > 54 < / c4 >
< c5 > 65 < ; / c5 >
< c6 > 78 < ; / c6 >
< / test >







所以我想

喜欢

manas 100 20

manas 33 54

manas 65 78



但是好像来了

manas 100 20

玛纳斯100 20

玛纳斯100 20

玛纳斯100 20

玛纳斯100 20





plz帮帮我。


拆分字符串特定字符wis e。这个问题。



尝试以下代码。

列表<串GT; result =  new  List< string>(); 
string str = * 123367676789990098877665655444444343445445555555555555555< /跨度>;
str = str.Substring(str.IndexOf(' *')+ 1 );
string ans = string .Empty;
while (str.Length> 0)
{
if (str.Length > 2
ans = * + str.Substring( 0 3 );
else
{
ans = * + str;
result.Add(ans);
break ;
}

result.Add(ans);
str = str.Substring(ans.Length - 1 );
}
< / string > < / string > ;


Dear sir,

My string is like
string teststr="*123367676789990098877665655444444343445445555555555555555";
i want to split the string taking first character(*) with 3 next character till last.
output like:

*123
*367
*676
*789
*990
*098
*877
*665
*655
*444
*444
*343
*445
*445
*555
*555
*555
*555
*555

plz help me.

解决方案

There are a couple of ways to do it - you could use a loop and the string.Substring method, or (possibly) a regex - but the last would probably want a loop or Linq method to "rebuild" the string with the "*" prefix.
The loop option is pretty simple:
Use string.IndexOf to find the "*" character.
Use a for loop, starting at the character after the star, ending at the string length, incrementing by three each time.
In the loop, use string.Substring to extract the three characters, and add them to a List of strings, together with the "*" prefix.


XmlDocument xmlDoc = new XmlDocument();
       xmlDoc.Load(@"D:\Prod.xml");

      XmlNode node = xmlDoc.DocumentElement.FirstChild;
      XmlNodeList lstVideos = node.ChildNodes;
      for (int i = 0; i < lstVideos.Count; i++)

       {

         

           for (int j = 0; j < 3; j++)

           {

               Response.Write("  " + lstVideos[j].InnerText + " ");

           }

           Response.Write("<br>");
       }







xml file is:

<?xml version="1.0"?>
-<DocumentElement>
-<test>
<c0>manas</c0>
<c1>100</c1>
<c2>20</c2>
<c3>33</c3>
<c4>54</c4>
 <c5>65</c5>
<c6>78</c6>
</test>




So i want
like
manas 100 20
manas 33 54
manas 65 78

but coming like
manas 100 20
manas 100 20
manas 100 20
manas 100 20
manas 100 20


plz help me.


split a string specific character wise for this question.

try following code.

List<string> result = new List<string>();
                        string str = "*123367676789990098877665655444444343445445555555555555555";
                        str = str.Substring(str.IndexOf('*') + 1);
                        string ans = string.Empty;
                        while (str.Length>0)
                        {
                            if (str.Length > 2)
                                ans = "*" + str.Substring(0, 3);
                            else
                            {
                                ans = "*" + str;
                                result.Add(ans);
                                break;
                            }

                            result.Add(ans);
                            str = str.Substring(ans.Length - 1);
                        }
</string></string>


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

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