.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

Please Check the following code

Posted By: Aditya Varma 999     Posted Date: March 17, 2014    Points:5   Category :C#
Here i want to divide the string into 3 parts

Please check the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SentencePrograms
{
class DividName
{
public void SplitString(string name,out string first,out string second)
{
int e=name.LastIndexOf(' ');
first = name.Substring(0, e);
second = name.Substring(e + 1);
}
}

class OutModifierDemo
{
static void Main(string[] args)
{
string message, first, second, mSplit;

DividName divName = new DividName();

Console.WriteLine("Enter any message");
message=Console.ReadLine();

mSplit = divName.SplitString(message, out first, out second);
Console.WriteLine(mSplit);
Console.WriteLine(first);
Console.WriteLine(second);
}
}
}


Responses
Author: Arun Radhakrishnan             
Posted Date: March 19, 2014     Points: 20   

From your code:

Return type of "SplitString" is void, but you are expecting some value from this function.
mSplit = divName.SplitString(message, out first, out second);



Try with the following code.
"DividName" is a kind of utility class (you may change the name to StringUtil) so you may declare "ParseName" as a static function.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class DividName
{
public static string[] ParseName(string name)
{
string[] names = new String[3];
if(name.Length > 0 && name.LastIndexOf(' ') > -1)
names = name.Split(' ');
return names;
}
}

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter any message");
string userName = Console.ReadLine().Trim();

string[] names = DividName.ParseName(userName);

Console.WriteLine("First Name:" + names[0]);
Console.WriteLine("Middle Name:" + names[1]);
Console.WriteLine("Last Name:" + names[2]);

Console.ReadLine();
}
}
}



Let me know if you need more details



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend