Regarding Strings in C#.Net

Posted by Subramanyame under Interview Questions on 1/5/2013 | Points: 10 | Views : 1816 | Status : [Member] | Replies : 3
Hi,


I have a string like as below

string str = " A + B - C * D"

I need an output As shown below:

o/p: ABCD




Responses

Posted by: Sinisreedhar on: 1/6/2013 [Member] Starter | Points: 25

Up
0
Down

string str = " A + B - C * D"
string[] chars = new string[] { ",", ".", "/", "!", "@", "#", "$", "%", "^", "&", "*", "'", "\"", ";","_", "(", ")", ":", "|", "[", "]" };
for(int i=0;i<chars.length;i++)
{
if(str.Contains(chars[i]))
{
str=str.Repalce(chars[i],"");
}

}

Ready to accept challenges at any time.....

Subramanyame, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raja_89 on: 1/7/2013 [Member] Starter | Points: 25

Up
0
Down
Hai

Steps

1.Store the list of special characters

2.Get the input

3. Check the string with the special characters using if and for loops

if => To check condition

if present replace that char..


Code:


string input="A*B+C-D%Q";

Console.WriteLine("Enter the string with chars",);

string[] setofchars= new string[] {"*","/","+","-","%"};

int i = 0;
for (i = 0; i < setofchars.Length; i++)
{

if (input.Contains(setofchars[i]))
{
input = input.Replace(setofchars[i], "");


}

}

Console.WriteLine(input);
Console.ReadLine();


O/P: ABCDQ



Regards








Subramanyame, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishwakottur-22149 on: 5/9/2013 [Member] Starter | Points: 25

Up
0
Down
Dim str As String = " A + B - C * D"
str = Regex.Replace(str, "[^\w\\]", "")

Subramanyame, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response