Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string[] array = new string[] { "12", "34", "5^67", "8^945", "^63^", "9484^" };
                    foreach(string message in array)
                    {
                              FindString(message)

                    }
 string remainingString = "";
 string tempString = "";
private void FindString(string message)
        {

            if(message.Contains("^"))
            {//remove char ^ process
                int pos = -1;
                pos = message.IndexOf('^');
                outputString = message.Substring(0, pos);
                remainingString = message.Substring(pos);
                if(tempString != "")
                {
                    outputString = tempString + outputString;
                    tempString = "";
                }
            }
            else
            {
                tempString = tempString + message;
            }

        }

From array i have to get messages 12345,678,945,63,9484. “^” shows end of message in string
Posted
Updated 1-Apr-13 21:39pm
v2
Comments
OriginalGriff 2-Apr-13 3:19am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
[no name] 2-Apr-13 3:39am    
From array i have to get messages 12345,678,945,63,9484.
[no name] 2-Apr-13 8:16am    
Sir Did u know about named Pipe in C# actually the code i have added for named pipe application...i will explain u in brief if u have time .:)

use String.join to get string of array and then use replace to remove ^ from string
example
C#
string[] array = new string[] { "12", "34", "5^67", "8^945", "^63^", "9484^" };
string myStr = String.Join("", array);
string temp = myStr.Replace("^", "");
textBox1.Text = temp;

or try this
C#
string[] array = new string[] { "12", "34", "5^67", "8^945", "^63^", "9484^" };
string finalOpStr = "";
foreach (string message in array)
{
     finalOpStr = finalOpStr + message.Replace("^", "");
}
finalOpStr = string.Format("9999,999,999,99,9999", finalOpStr).ToString();
textBox1.Text = finalOpStr;


if u mean o/p 12345,678,945,63,9484 is list items separated by comma...u want get this messages by separating ^ and store in list array and as u want use for loop for it ...then in ur main code there is need to do some changes exa.
C#
string[] array = new string[] { "12", "34", "5^67", "8^945", "^63^", "9484^" };
            List<string> myString=new List<string>();
            foreach (string message in array)
            {

                if (message.Contains("^"))
                {//remove char ^ process
                    string message1 = message;
                    while (message1.Contains("^") || message1!="")
                    {
                        int pos = -1;
                        pos = message1.IndexOf('^');
                        if (pos != -1)
                        {
                            outputString = message1.Substring(0, pos);
                            message1 = message1.Substring(pos + 1);
                            if (tempString != "" || outputString != "")
                            {
                                outputString = tempString + outputString;
                                myString.Add(outputString);
                                tempString = "";

                            }
                        }
                        else
                        {
                            tempString = message1;
                            message1 = "";
                        }
                    }
                }
                else
                {
                    outputString = outputString + message;
                    tempString = tempString + message;
                }

            }
           textBox1.Text = string.Join(",",myString.ToArray());


i think it will help u.
 
Share this answer
 
v3
Comments
Maciej Los 2-Apr-13 3:48am    
Nice, +5!
Pallavi Waikar 2-Apr-13 3:57am    
Thanks sir.
[no name] 2-Apr-13 3:54am    
I have for loop Pallavi
Pallavi Waikar 2-Apr-13 3:57am    
String.join is used at the place of for loop..they are equivalent in this case for array
[no name] 2-Apr-13 4:05am    
Pallvi i dont have to pass array.. i want to pass string in array because my situation is diffrent for understanding purpose i have taken array.
You don't need to create custom function to replace any sign. Please, see this: String.Replace() method[^].

C#
foreach(string message in array)
 {
         message = message.Replace("^",string.Empty);
         //process with  message ;)
 }


[EDIT]
You need to convert string array into string. How to do that? http://www.dotnetperls.com/convert-string-array-string[^]
I recommend you to use StringBuilder Class[^]

Example:
C#
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] array = new string[] { "12", "34", "5^67", "8^945", "^63^", "9484^" };
            Console.WriteLine(StringArrayToString(array));
            Console.ReadKey();
        }


        private static string StringArrayToString(string[] sarray)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (string ele in sarray)
            {
                sb.Append(ele.Replace("^",","));
            }
            return sb.ToString();

        }

    }
}


Result: 12345,678,945,63

[/EDIT]
 
Share this answer
 
v4
Comments
[no name] 2-Apr-13 3:40am    
From array i have to get messages 12345,678,945,63,9484.
Maciej Los 2-Apr-13 3:47am    
See my updated answer.
[no name] 2-Apr-13 8:15am    
Sir Did u know about named Pipe in C# actually the code i have added for named pipe application...i will explain u in brief if u have time .:)
Maciej Los 2-Apr-13 8:38am    
If you have any issue, please, post it as a new Question.
Maciej Los 2-Apr-13 10:35am    
Please, see updated answer (example added).

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