0

I have a array with values and want to append a link to all the values of array assign back the value to same array.

string [] files = null; // will contain array of string values

string[] attachmentFilePath = files;
string[] attachmentFileName = files;

I want to append "http://www.google.com" with every value in the files array and assign it to attachmentFilePath.

I have tried a lot using string.format("google.com",files[index])

for(var i = 0; i<files.count();i++)
{
    files[index] = string.format("http://www.google.com",files[index]);
}

tried a lot but some or the other way the code gives error or index out of bounds or null reference exception.

I need the string to appended like 'http://www.google.com/files.value'

Can anyone help me out ?

6
  • 1
    How do you want to append it? just at the end of each string? with a space? please show format. In addition please show what you have tried.. Commented Feb 11, 2018 at 19:25
  • I have tried a lot using string.format("google.com",files[index]) Commented Feb 11, 2018 at 19:26
  • 1
    Please add the code you have tried to the question. having index I guess it includes also a loop Commented Feb 11, 2018 at 19:27
  • In addition what is the desired format? Commented Feb 11, 2018 at 19:27
  • 1
    Please give us example content and desired output (at least a few lines/itterations). Right now your question is less then clear. Commented Feb 11, 2018 at 19:28

2 Answers 2

1

Using string.Format requires the string to be in the proper format for formatting:

string.Format("some string with place holder: {0}","some string to put");

If your string does not have the placeholders (as in your case) it doesn't do anything. Read more about string.Format

Solutions:

  1. Simple for loop:

    var yourString = "http://www.google.com/";
    var attachmentFilePath = new string[files.Length];
    for(int i = 0; i < files.Length; i++)
    {
        attachmentFilePath[i] = yourString + files[i];
    }
    
  2. Linq:

    var yourString = "http://www.google.com/";
    var attachmentFilePath = files.Select(s => yourString + s).ToArray();
    

And of course you can correctly use string.Format to any of these two solutions where appending the strings. Just see it is has the place holder in the place you want

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

3 Comments

minor but I think you meant s => yourString + "/" + s ? as the OP wants it appended at the end of http://www.google.com.
the concatenation is still the wrong way around I think it should be yourString + s instead of s + yourString and same for the first example. should be yourString + files[i] i believe :)
@Aominè - haha true. Stupid of me. Long day
0

You can accomplish the task at hand with something along the lines of:

string[] attachmentFilePath = files.Select(x => $"http://www.google.com/{x}")
                                   .ToArray();

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.