Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I have one sting

string = abc 10 20 10 10 10 pqr 10 20 10 10 10 wer 10 10 10 10 10 tryfhh 20 20 30 30 30

how can i split into line as a
abc 10 20 10 10 10
pqr 10 20 10 10 10
wer 10 10 10 10 10
tryfhh 20 20 30 30 30
Posted
Comments
[no name] 21-Aug-14 9:27am    
Looks like you might just have to iterate through your string and then substring out the parts you want.

How about this?

C#
string source = "abc 10 20 10 10 10 pqr 10 20 10 10 10 wer 10 10 10 10 10 tryfhh 20 20 30 30 30";
string regExp = "[a-z]+";
Regex regx = new Regex(regExp);
MatchCollection collection = Regex.Matches(source, regExp);
for (int index = 1; index < collection.Count; index++)
{
    source = source.Replace(collection[index].Value, System.Environment.NewLine + collection[index].Value);
}
Console.WriteLine(source);
 
Share this answer
 
Comments
Herman<T>.Instance 21-Aug-14 10:35am    
My 5! Well thought about using the regular expression. For people who do not understand this code. Copy it to a VS project (Console App) and debug. You see the magic!

Magic:
Regex regx = new Regex(regExp);
MatchCollection collection = Regex.Matches(source, regExp);
hypermellow 21-Aug-14 10:44am    
Have my 5 too! ... that's a really nice solution :-)
Member 10548197 22-Aug-14 5:02am    
THAN K YOU SIR BUT I have also one question how to store this new line in a hash table and the key field is
key value
abc abc 10 20 10 10 10
pqr pqr 10 20 10 10 10
wer wer 10 10 10 10 10
tryfhh tryfhh 20 20 30 30 30
_Asif_ 22-Aug-14 5:18am    
Thats easy, split the string using string[] tmp = source.Split('\n'), you already have captured your keys in MatchCollection. Just traverse the array and add key and string into hashtable.
.... or another approach using int.TryParse as the test to continue the current line.


C#
string sOutput = string.Empty;
string sInput = "abc 10 20 10 10 10 pqr 10 20 10 10 10 wer 10 10 10 10 10 tryfhh 20 20 30 30 30";
string[] sStringArray = sInput.Split(' ');
StringBuilder sb = new StringBuilder();
int iTmp = int.MinValue;

for(int i = 0; i<sStringArray.length;i++){
    if (int.TryParse(sStringArray[i], out iTmp))
    {
        //If the current array value is a number, then continue the current line ....
        sb.Append(sStringArray[i]);
        sb.Append(" ");
    }
    else
    {
        //Current array value is not a number
        if (i == 0)
        {
            // on the 1st iteration, we don't need a new line before the text value ...
            sb.Append(sStringArray[i]);
        }
        else
        {
            // on subsequent iterations, we need a new line before the text value ...
            sb.Append(Environment.NewLine);
            sb.Append(sStringArray[i]);
            sb.Append(" ");
        }
    }
}
sOutput = sb.ToString();
sb = null;



Hope it helps.
 
Share this answer
 
v2
you can use IndexOf.

C#
int index = myString.IndexOf(' ');
int count=0;
while (index >= 0)
{
if (count == 5)
{
// take a substring of the string using index value and store it as you want
count=0;

}
    count++;
    index  = myString.IndexOf(' ', index + 1);
}
 
Share this answer
 
Comments
Herman<T>.Instance 21-Aug-14 10:31am    
you assume a thing. What if the count should be 6 because string does not match your thought pattern?
Based on your sample data, assuming it is an accurate representation of what you are going to get.

I would start by splitting your initial string based on a space.

So:
C#
var splitString = string.Split(' ');

Then, looking at the data each "batch" contains 6 entries.
So I would then do something like this

C#
var res = new List<string>()
var tempBuilder = new StringBuilder();
for(int i = 0; i < splitString.Length; i++)
{
  tempBuilder.Append(splitString[i]);
  tempBuilder.Append(" ");
  if(i%6 == 0)
  {
      res.Add(tempBuilder.ToString().Trim();
      tempBuilder = new StringBuilder();
  }
}</string>


Then when it finishes you should have an array of strings as per your request.

The code can probably be tidied up quite a bit as I was just typing as I thought but it should give you an idea.
 
Share this answer
 
Comments
Herman<T>.Instance 21-Aug-14 10:32am    
you assume a thing. What if the count should be 6 because string does not match your thought pattern?
Pheonyx 21-Aug-14 10:36am    
I did state on my answer that it is based on the sample provided being how the data will be presented. However, if it isn't the case, then you would have to look for some sort of indicator to identify how long the line should be, for example you could incorporate a part of Solution 3. to check if you need to do a new line instead of modding against the index for 6.
Herman<T>.Instance 21-Aug-14 10:39am    
see solution 4. That's awesome solution
Pheonyx 21-Aug-14 10:46am    
It's a different way of doing a similar thing. Again he is making assumptions, that regex wouldn't work for capital letters. And what if the user want's a collection of strings and not a string with line breaks in. I do agree it is a nice and tidy solution.

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