Home > Net >  How to add a unique string before and after a character in a string
How to add a unique string before and after a character in a string

Time:01-20

I have a string "AAAA", and I need to add certain lines before each character in this string. For example, given

string original = "AAAA". 
string firstStringBeforeChar = "B"
string firststringAfterChar = "C"

and after the conversion, I want to get a string:

string converted = "BACAAA"

For each index, the original string will have its own stringBeforeChar and stringAfterChar. The final output should be like this:

string converted = "BACBACBACBAC"

Where B and C are a unique string for each character in string original. How do I do this?

CodePudding user response:

You can use a loop to iterate through each character in the original string, and use the StringBuilder class to append the appropriate strings before and after each character

Example:

using System.Text;

string original = "AAAA";
string[] stringBeforeChar = {"B", "D", "E", "F"};
string[] stringAfterChar = {"C", "G", "H", "I"};

StringBuilder sb = new StringBuilder();

for (int i = 0; i < original.Length; i  )
{
    sb.Append(stringBeforeChar[i]);
    sb.Append(original[i]);
    sb.Append(stringAfterChar[i]);
}

string converted = sb.ToString();
Console.WriteLine(converted);
Console.WriteLine(converted);

Output:

BACDAGEAHFAI

CodePudding user response:

You can do this in a single line of code.

string converted = original.Replace(original[0].ToString(), $"{firstStringBeforeChar}{original[0]}{firststringAfterChar}");

CodePudding user response:

You can do like this, if that's what you are asking:

    string original = "AAAA";
    string firstStringBeforeCharacter = "B";
    string firstStringAfterCharacter = "C";
    var result = new StringBuilder();
    foreach(var c in original)
    {
        result.Append($"{firstStringBeforeCharacter}{c}{firstStringAfterCharacter}");
    }

    Console.WriteLine(result.ToString());

CodePudding user response:

You can use a loop to iterate through each character in the original string, and for each character, concatenate the unique string before and after it, and append it to a new string. The StringBuilder class can be used to efficiently build the new string. Here's an example implementation in C#:

string original = "AAAA";
string firstStringBeforeChar = "B";
string firstStringAfterChar = "C";

StringBuilder sb = new StringBuilder();

foreach (char c in original)
{
    sb.Append(firstStringBeforeChar);
    sb.Append(c);
    sb.Append(firstStringAfterChar);
}

string converted = sb.ToString();

If you want to use different stringBeforeChar and stringAfterChar for each character you can use another loop and use an index counter to access the index of the current character, you can have an array of stringBeforeChar and stringAfterChar and use the index to access the current stringBeforeChar and stringAfterChar.

string[] stringBeforeChar = new string[] { "B", "D", "E", "F" };
string[] stringAfterChar = new string[] { "C", "G", "H", "I" };

for (int i = 0; i < original.Length; i  )
{
    sb.Append(stringBeforeChar[i]);
    sb.Append(original[i]);
    sb.Append(stringAfterChar[i]);
}

This will add unique string before and after each character of original string.

CodePudding user response:

If you want to add before and after character you can try Linq: for each character c we decide what to put into result and then Concat all our decisions into the finla string.

If you want to add before certain char, here it is 'A':

using System.Linq;

...

var result = string.Concat(original 
  .Select(c => c == 'A'
             ? $"{firstStringBeforeChar}{c}{firststringAfterChar}"
             : $"{c}"));

If you want to add before / after any char, drop the condition:

using System.Linq;

...

var result = string.Concat(original 
  .Select(c => $"{firstStringBeforeChar}{c}{firststringAfterChar}"));
  •  Tags:  
  • c#
  • Related