0

How can I get the output from the following method?

public static string getLimitedWords(string str,int NumberOfWords)
{
  string[] Words= str.Split(' ');
  string _return=string.Empty;                 

  if(Words.Length<=NumberOfWords)
  {
        _return = str;
  }
  else
  {
        for(int i=0;i<NumberOfWords;i++)
        {
              _return+=Words.GetValue(i).ToString()+" ";
        } 
  } 
  return _return.ToString();
}
3
  • 3
    What does "display the method" mean? Commented Sep 28, 2009 at 11:55
  • 1
    Display where? Do you have another question? Commented Sep 28, 2009 at 11:55
  • Jordie91 doesn't seem to be a native English speaker ; it's highly probable that "display" is the literal translation of a dutch word... Commented Sep 28, 2009 at 12:38

4 Answers 4

1

If debugging, try System.Diagnostics.Debug.Write(getLimitedWords(yourString,yourNumberOfWords));

If using ASP.NET, try Page.Response.Write(getLimitedWords(yourString,yourNumberOfWords));

If using Console, try System.Console.Write(getLimitedWords(yourString,yourNumberOfWords));

Sign up to request clarification or add additional context in comments.

1 Comment

Or, if in the debugger, just hover the mouse over the returned value.
0

Such a poorly asked question, but I can't resist optimizing your method:

public static string getLimitedWords(string str,int NumberOfWords)
{
    return String.Join(" ",str.Split(' ').Take(NumberOfWords).ToArray());
}

As for the answer to your question, I can't figure out what you're asking. Rephrase the question and you might get a more useful answer.

2 Comments

Where did that Take function come from?
Great, now he probably has to upgrade to the latest .Net framework to use your optimized method :)
0

The output of the method is a string. Which means that depending upon your platform, you can display it any number of ways. The following snippet from a C# Console application is one such way:

string str =  "This is a String of Words";
int numberOfWords = 5;
Console.WriteLine(getLimitedWords(str, numberOfWords));
Console.Read();

The output is:
This is a string of

You can also assign the output to a string and display that any number of ways:

string output = getLimitedWords(str, numberOfWords);
Console.WriteLine(output);

Comments

0

I'm just trying to guess what you need here:

String answer = getLimitedWords( str, NumberOfWords);

Console.WriteLine ( answer );

Could it be something like this?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.