tags:

views:

98

answers:

7

Hi,

i have following piece of code with me iam spliting the words using only one space and the output is

output:

when 
ambition
ends
happiness
begins

But i want split the words after two spaces i mean my output should be like this:

when ambition
ends happiness
begins
string vj = "when ambiton ends happiness begins";
List<string> k = new List<string>();
char ch = ' ';
string[] arr = vj.Split(ch);
foreach (string r in arr)
{
  Response.Write(r + "<br/>");
}
A: 

You're gonna have to go manual on this one, searching for the space-character in the string, and keeping a counter. You'll have to remember the indices of the space-characters you'll want to split from, and use the .Substring(startindex, length) method for extracting the separate parts.

Joachim VR
Dunno about that. The OP has already split it. Just means the OP has to go two tokens at a time instead of one. (i.e. Don't use foreach)
Kirk Woll
You could of course split them like in the example, and just paste them together again, if you're not afraid of a little extra string creations.
Joachim VR
+2  A: 

As this is marked homework, I will give a hint:

  • Keep a counter for how many spaces you do want in each line
  • Reset this counter when number of spaces is reached
  • When number of spaces is reached, add a line break
Oded
+1  A: 

Split it like you do right now, then loop the resulting array and join them all up using a standard for loop.

Deniz Dogan
why create so much work, wen only a small index will suffice?
loxxy
@loxxy: Coding wise it's little work, but performance wise I suppose it could be better. Not that I think that performance is an issue here...
Deniz Dogan
A: 
  1. Create a variable containing start position of words to extract
  2. Create a space counter
  3. Loop through char by char (using an indexer)
  4. When the number of spaces equals two, call substring method from position saved in #1 to current index.
  5. Change variable from #1 to the current index.
jgauffin
A: 
string vj = "when ambiton ends happiness begins";
List<string> k = new List<string>();
char ch = ' ';
string[] arr = vj.Split(ch);
bool flag=false;

foreach (string r in arr)
{
 if(flag)
    {
    Response.Write(r + "<br/>");
    flag=false;
    }
 else
    { 
    Response.Write(r + " ");
    flag=true;
    }
}

The above is good for the case you specified (skipping a single space).


Though not necessary, but if you want it all wrapped up, Here :

string myString = "when ambiton ends happiness begins";

bool flag=false;

foreach (string item in myString.Split(ch))
         Response.Write(item + flag=!flag?" ":"<br/>");
loxxy
What is `List<string> k` for? Your variable naming remembered me my computer science school teacher, who almost screamed at anyone for variables named like a,b,v,... :)
Kirill Muzykov
... Since you already told the answer.. Why not flag= !flag ?
Oren A
Thanks very much............
@Kirill - I edited directly in the OP's code. The variable names are not my choice.
loxxy
Oops sorry. Put comment at the wrong place.
Kirill Muzykov
A: 

You can just add a bool indicating whether or not you should add "/br", call it breakLine.
Now at each iteration check if it's true - if so add "/br", otherwise don't. finally negate it at each iteration.
Implementation is left as an exercise (-:

Oren A
+2  A: 

Here's a solution with regular expressions:

// Loosely: a word, followed by another word if available
Regex regex = new Regex(@"\S+( \S+)?"); 

string[] splits = regex.Matches(inputText)
                       .Cast<Match>()
                       .Select(match => match.Value)
                       .ToArray();
Ani
+1 for creativity. But you did see it's homework, right?
Oren A
Thanks very much
sorry its not home work im bit beginner to this and working on data mining project so got stucked here so i needed help.
@Oren A: Whoops. I began to work on it before the tag was added. Thanks for letting me know.
Ani