How to use string interpolation in foreach loop ?

Binumon George 21 Reputation points
2021-12-23T04:50:08.96+00:00

Hi All,
Please help me to format the string with 'string Interpolation'. Below is my foreach loop code with string + concatenation. I want to avoid + concatenation and use 'string Interpolation'

        int itemsCount = oUpdateVarList.Count();
        int loopCount = 0;
        foreach (string oVarItem in oUpdateVarList)
        {
            loopCount++;
            string[] oArrList = oVarItem.Split(new[] { oEngineContext.colDelimiter }, StringSplitOptions.RemoveEmptyEntries);

            if (loopCount != itemsCount)
            {
                query += oArrList[1] + " : '" + oArrList[3].ToString() + "',";
            }
            else
            {
                query += oArrList[1] + " : '" + oArrList[3].ToString()+ "'}) return id(a)"; 
            }

        }
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2021-12-23T06:13:17.747+00:00

    @Binumon George , you could try the following code to use string interpolation to replace string + concatenation.

       int itemsCount = list.Count();  
        int loopCount = 0;  
        string query = string.Empty;  
        foreach (string oVarItem in list)  
        {  
            loopCount++;  
            string[] oArrList = oVarItem.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);  
      
            if (loopCount != itemsCount)  
            {  
                query=$"{query}{oArrList[1]}: '{oArrList[3].ToString()}',";  
           
            }  
            else  
            {  
                query=$"{query}{oArrList[1]}: '{oArrList[3].ToString()}'}}) return id(a)";  
            }  
        }  
    

    Based on my test, I can get the same result with the code you provided.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. P a u l 10,761 Reputation points
    2021-12-23T06:09:22.9+00:00

    You just need to add a $ before your string, then you can embed your variable between { and }:

    int itemsCount = oUpdateVarList.Count();
    int loopCount = 0;
    foreach (string oVarItem in oUpdateVarList) {
        loopCount++;
        string[] oArrList = oVarItem.Split(new[] { oEngineContext.colDelimiter }, StringSplitOptions.RemoveEmptyEntries);
    
        if (loopCount != itemsCount) {
            query += $"{oArrList[1]} : '{oArrList[3]}',";
        } else {
            query += $"{oArrList[1]} : '{oArrList[3]}'}}) return id(a)";
        }
    }
    

    Notice that if you have any literal brace characters in your string then you have to escape them by double them up, i.e. } becomes }} to let the compiler know that it's not an interpolated variable.

    Also variables that you embed in an interpolated string are automatically .ToString()'d (so does string.Format too actually) so you can avoid adding those in.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.