Saeed Khan
Saeed Khan

Reputation: 537

How to Join Array of string into single string?

Scenario: I have list of Networks Name in Database Table with numbers e.g (1. Facebook, 2. Twitter, 3. MySpace, 4. hi5 ...) and I select one Network from database (e.g 2. Twitter).

What I Did:

string Selected = "12.FaceBook";
int k=3;
string[] myArray = new string[Selected.Length];
for (int i = 0; i < Selected.Length; i++)
{
    myArray[i] = Selected[k].ToString();
    k++;
}

and sucked how to join myArray and print in

DevComponents.DotNetBar.MessageBoxEx.Show("?");

What I Want:

output as:

"Facebook" or "Twitter" without numbers.

Upvotes: 4

Views: 10380

Answers (7)

mipe34
mipe34

Reputation: 5666

You can do it using string.Join method to join the strings,"string".Split to split out the number and string.Format to add " apostrophes.

var list = new[] {"1. Facebook", "2. Twitter", "3. MySpace"};

var result = string.Join(" or ",list.Select(s => string.Format("\"{0}\"", s.Split('.')[1].Trim())).ToArray());
DevComponents.DotNetBar.MessageBoxEx.Show(result);

You can try it here.

Upvotes: 0

wsnzone
wsnzone

Reputation: 51

maybe you need static string.Join?

or

var input="12. Twiiter";
var re=new Regex(@"(?<num>\d+)\.\s*(?<code>.+)");
var m=re.Match(input);
if (m.Success) Console.Write(m.Groups["code"].Value);

sorry,

var input=new []{"12. ...","9. ..."}
var output = input.Select(x=>string.Join(x.Split(".").Skip(1),".").Trim());

Upvotes: 0

kostas ch.
kostas ch.

Reputation: 2035

Use regex

string s1 = Regex.Replace(Selected, "[^A-Za-z]", "");

Upvotes: -1

dtb
dtb

Reputation: 217313

If you have a string like "12. Facebook" then you can easily get the part after the . using the IndexOf Method and the Substring Method as follows:

string input = "12. Facebook";

string result = input.Substring(input.IndexOf('.') + 1)
                     .Trim();
// result == "Facebook"

Upvotes: 1

Matthew Watson
Matthew Watson

Reputation: 109597

This should do it:

string joined = string.Join("", myArray);
DevComponents.DotNetBar.MessageBoxEx.Show(joined);

If you want to put a separator between the joined strings, that's the first parameter of string.Join(). For example, to put a space between them:

string joined = string.Join(" ", myArray);

However, your code to actually create the string array in the first place looks wrong. Do you get a single string back from the database for the required network, or do you get a single string containing all networks that you have to parse yourself?

Upvotes: 6

Tim Schmelter
Tim Schmelter

Reputation: 460158

Maybe simply

var companiesWithNumber = new[] { "1. Facebook", "2. Twitter", "3. MySpace" };
var companiesWithoutNumber = companiesWithNumber.Select(c => c.Split().Last());

Demo

If you need an array:

string[] result = companiesWithoutNumber.ToArray();

Upvotes: 0

Saeed Khan
Saeed Khan

Reputation: 537

I have tried and I Succeeded here is Answer for that, only little adding e.g Selected.Length-3 and printing output.

Here is My Code

try
                {
                    int i;
                    string output = "";

                    string Selected = "12.FaceBook";
                    int k = 3;
                    string[] myArray = new string[Selected.Length];
                    for (i = 0; i < Selected.Length-3; i++)
                    {
                        myArray[i] = Selected[k].ToString();

                        output = output + myArray[i];
                        k++;
                    }


                    DevComponents.DotNetBar.MessageBoxEx.Show(output);
                }
                catch (Exception ee)
                {

                }

Upvotes: 0

Related Questions