4

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.

1
  • 3
    What are you actually after? Your question title asks how to join, but your message is splitting a string into individual characters, and your example seems to want to split on '.' Commented Jun 11, 2013 at 11:59

7 Answers 7

6

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?

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

6 Comments

"What I want is ouput as "Facebook" or "Twitter" but with out numbers" However, the question is not clear at all.
thanks Guys almost all answer are correct but I liked this one most.
"your code to actually create the string array in the first place looks wrong" +1
@SaeedKhan Your question is a bit ambiguous though, so I wasn't sure if I was really answering it...
Sorry this gives me "" string
|
1

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"

Comments

0

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)
                {

                }

1 Comment

This assumes that the input string always starts with a 2-digit number (like 12.) but not with more or less digits (1. or 1000.) Is that the case?
0

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();

Comments

0

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());

Comments

0

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.

Comments

-1

Use regex

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.