Hi everybody, I need a little help regarding string. I want to break the string. The example is given following.
STRING: AL HAMZA SHIP BREAKING CO
INTO:
AL
HAMZA
SHIP
BREAKING
CO
Please send me the code for breaking the sentance.
Regards
Zeb
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Dec 7, 2011, 1:15 AM EST
If you don't do this, then the array produced by String.Split will contain an empty string for each surplus space.
Satyapriya NayakRank #47 · 31.5K reputationPosted Dec 7, 2011, 12:41 AM EST
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "AL HAMZA SHIP BREAKING CO";
string[] words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
Console.ReadLine();
}
}
}
Thanks
Pravin MoreRank #1,419 · 1.2K reputationPosted Dec 7, 2011, 12:52 AM EST
use like this.....
string strsplit = "my name is Zeb";
string[] lines = strsplit.Split(new string[] { " ", "\n" }, StringSplitOptions.None);
MessageBox.Show(lines[0].ToString()); //will give "my"
thanks,
Pravin.
Zeb Rehman41 reputationPosted Dec 8, 2011, 9:49 AM EST
Zeb Rehman41 reputationPosted Dec 8, 2011, 9:50 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Dec 8, 2011, 6:03 PM EST