Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a string as,

C#
string strString = "1 I am good and 2 how are you"


I processed the above string in character array as below,

C#
Char[] charReader = new Char[strString .ToCharArray()];


In the above character array, i am including a string as, were ever the number present i am inserting a string along with it,

expected o/p should be 1 htmltag I am good and 2 htmltag how are you

C#
string strInclude = "htmltag";
Char[] charInclude = new Char[strInclude.ToCharArray()];    


How to insert or serialize the characters to insert a string next to number
Posted
Updated 13-Nov-14 23:51pm
v2
Comments
Sinisa Hajnal 14-Nov-14 6:08am    
What html tag, what are you trying to do?
riodejenris14 14-Nov-14 6:51am    
Kindly look into this article http://www.codeproject.com/Questions/842430/Insert-a-string-inside-node-name-using-csharp?arn=0

That's complicated, because you don't want to go anywhere near character arrays to do this: they are harder to work with for inserts than strings are!

Start with identifyign teh location of your "insert points" - in this case the numbers.
As simple regex will do that for you:
\d+\s

The Rexex Match includes the Index property[^] which tells you where in the string it found the number.
You can then use the index with the string.Substring method to break the string in two, and build a new one from the parts:
C#
string old = "Abcd";
int index = 2;
string new = old.Substring(0, index) + "xx" + old.Substring(index);
Will give you a new string containing "Abxxcd".

You will probably want to work from the last match backwards...
 
Share this answer
 
Comments
riodejenris14 14-Nov-14 6:51am    
Kindly look into this article http://www.codeproject.com/Questions/842430/Insert-a-string-inside-node-name-using-csharp?arn=0
OriginalGriff 14-Nov-14 7:07am    
Why?
It's an unrelated question, isn't it?
And if you are trying to do something with XML, then playing with the strings directly is a very poor idea...
riodejenris14 14-Nov-14 7:09am    
Yes I dropped the idea I tried but finally the expected o/p does'nt come. So I dont no what to do, kindly check and let me know.
Thanks in advance.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string strString = "1 I am good and 2 how are you";

            Char[] charReader =strString.ToCharArray();

            Console.WriteLine();

            string strInclude = "htmltag";

            Char[] charInclude =strInclude.ToCharArray();

            foreach (char ch in charReader)
            {
                if (char.IsNumber(ch) == true)
                {
                    Console.Write(ch+" "+ strInclude);
                }
                else
                   Console.Write(ch.ToString());
            }
            Console.ReadLine();
        }
    }
}
 
Share this answer
 
Comments
riodejenris14 14-Nov-14 7:07am    
kindly check the article http://www.codeproject.com/Questions/842430/Insert-a-string-inside-node-name-using-csharp?arn=0
Help me pls...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900