Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

String manipulation

Posted on 2005-04-27
3
Medium Priority
?
176 Views
Last Modified: 2010-04-16
Hello

I have a string and a 'variable' length. I want to divide the string into the specified length and store it in an array.

For ex: string = "Experts Exchanges"  and length = 4;

the ArrayList Should contain:
Expe
rts
Exch
ange
s

How can I do this?

Thanks
Praveen
0
Comment
Question by:praveenuni
3 Comments
 
LVL 22

Accepted Solution

by:
cookre earned 1600 total points
ID: 13881959
idx=0;
while (s.Length>4)
         {
         arr[idx++]=s.Substring(0,4);
         s=s.Substring(4);
         }
if (s.Length>0) arr[idx]=s;
0
 
LVL 1

Assisted Solution

by:pbowens
pbowens earned 400 total points
ID: 13882070
You could also do the same thing with regex..

using System.Text.RegularExpressions;

string test = "Welcome to the experts exchange";

Regex re = new Regex(".{4}");
foreach (Match m in re.Matches(test))
{
   Console.WriteLine(m.ToString());
}
0
 
LVL 13

Expert Comment

by:dungla
ID: 13882120
based on cookre solution:

string strEE = "Experts Exchanges";
ArrayList arrOut = new ArrayList();
while (strEE.Length > 4)
{
      arrOut.Add(strEE.Substring(0, 4));
      strEE = strEE.Substring(4);
}
if (strEE.Length > 0)
      arrOut.Add(strEE);
for (int i = 0; i < arrOut.Count; i++)
{
      Console.WriteLine(arrOut[i]);
}
Console.ReadLine();
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question