Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am not able to replace the following string format

from

"9780810152205 (pbk. : alk. paper)||9780810152205(pbk. : alk. paper)"

to

"9780810152205";"(pbk. : alk. paper)";""||"9780810152205";"(pbk. : alk. paper)";""

The above strings need to splitted on the basis of ( or somthing else. the above string text keeps on changing so cannot apply directly..

It will be really helpfull if someone provide me code which changes the above string to needed format

Thanks
Nilesh
Posted

1 solution

Yes, you can just split it and then compose again; for split, use string.Split, to compose the array of sub-string back to one string (with proper replacement), use string.Format. Do not use concatenation "+", due to poor readability (easy to make a mistake) and pure performance (because strings are immutable). See http://msdn.microsoft.com/en-us/library/system.string.aspx[^].

This method is maybe not so elegant. Many would prefer using Regular Expressions. You will need to use one of the methods System.Text.RegularExpressions.Regex.Replace, see http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx[^].

You will need to learn Regular Expressions though, but it can be useful for other cases. See:
http://en.wikipedia.org/wiki/Regular_expression[^],
http://en.wikipedia.org/wiki/Regular_expression_examples[^].

This is a great tool to use: http://www.ultrapico.com/Expresso.htm[^].

—SA
 
Share this answer
 
v2
Comments
nileshrathod 17-Nov-11 1:21am    
i tried using string.split and string.contains but didnt achieved... so can u plz provide code for same..
nileshrathod 17-Nov-11 1:24am    
i also tried with this logic but didnt achieved the needed format.. i am pasting my code so can you plz let me know the changes in below menitioned code....

string data = "9780810152205 (pbk. : alk. paper)||9780810152205(pbk. : alk. paper)";

//string data = "9780810152205";"(pbk. : alk. paper)";""||"9780810152205";"(pbk. : alk. paper)";""

string data1 = ""; if (data == null) {

return ""; } else {

data.Replace(" (", "(");

string[] arr = data.Split('('); data = "";

foreach (string arr1 in arr) {

data += "\"" + arr1 + "\";";

if (arr1.Contains(")")) {

int v = data.Length - data.LastIndexOf("\"");

data = data.Remove(data.Length, v);

string abc = arr1.Replace("||", "|");

string[] arr3 = abc.Split('|');

data1 = "";

foreach (string arr4 in arr3) {

if (arr4.Contains(")")) {

data1 += "(" + arr4 + "\";" + "\"" + "\"";

} else

{

data1 += "||" + "\"" + arr4 + "\";";

} } }

data += data1; }

data = data.Replace(data.LastIndexOf(";").ToString(), "");

data = data.Remove(data.LastIndexOf(";"));

txtBooktitle1.Text = data; return data;

}

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