Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
What have you tried?
MuhammadUSman1 3-Jul-13 4:35am    
I have added solution please check my solution.

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.
 
Share this answer
 
Comments
connect2manas 2-Jul-13 15:19pm    
thanks for your answer.
i want a loop.
plz help me.
OriginalGriff 2-Jul-13 15:22pm    
Which bit is a problem?
XML
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
<?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.
 
Share this answer
 
Comments
MuhammadUSman1 3-Jul-13 0:55am    
Post it as separate question. not in solution
split a string specific character wise for this question.

try following code.
C#
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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900