Appending values if strings are not null or empty

Gunjan Arora 41 Reputation points
2022-02-23T09:19:40.727+00:00

Hi Folks,

I have three values for example string 1 , string 2,string 3
Now if string 1="uu",string 2="pp",string 3= "cc"
I want to concatenate something like this uu/pp(cc)
Now the problem is if string 1 is empty or null then I don't want to append / before string 2,similarly if string 3 is empty I don't want to concatenate parenthesis around it .

For example if string 1 and string 3 are empty then my output should be pp
I want to achieve this as a method as I have to repeat this for multiple inputs

Please suggest

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,819 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 28,031 Reputation points
    2022-02-23T11:42:52.23+00:00

    Honestly, this is fundamental programming logic. You've already figured the hard part which is the use case. From there just write the code just like the use case reads.

        public static string FormatString(string string1, string string2, string string3)
        {
            string results = string.Empty;
    
            if(!string.IsNullOrEmpty(string1))
            {
                results += $"{string1}/";
            }
    
            if (!string.IsNullOrEmpty(string2))
            {
                results += $"{string2}";
            }
    
            if (!string.IsNullOrEmpty(string3))
            {
                results += $"({string3})";
            }
    
            return results;
        }
    
    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,401 Reputation points
    2022-02-23T12:29:58.597+00:00

    @AgaveJoe solution is best for static value count e.g. Now if string 1="uu",string 2="pp",string 3= "cc"

    If by chance there can be a variable amount of strings consider

    private string ConcatenateValues(params string[] values)  
    {  
        string ReplaceLastOccurrence(string source, string find, string replace)  
        {  
            int index = source.LastIndexOf(find, StringComparison.Ordinal);  
            return index == -1 ? source : source.Remove(index, find.Length).Insert(index, replace);  
        }  
      
        var result = string.Join("/", values.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => $"{x}")) + ")";  
        return result.Length == 1 && result == ")" ? "" : ReplaceLastOccurrence(result, "/", "(");  
    }  
    

    Usage

    Console.WriteLine(ConcatenateValues("aa", "bb", "cc", "dd", "pp"));  
    Console.WriteLine(ConcatenateValues("aa", "", "cc"));  
    Console.WriteLine(ConcatenateValues("aa", "bb", ""));  
    Console.WriteLine(ConcatenateValues("", "bb", "cc"));  
    Console.WriteLine(ConcatenateValues("aa", "bb", "cc"));  
    Console.WriteLine($"'{ConcatenateValues()}'");  
    

    Results

    aa/bb/cc/dd(pp)  
    aa(cc)  
    aa(bb)  
    bb(cc)  
    aa/bb(cc)  
    ''  
    
    1 person found this answer helpful.
    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.