How to call String.Split that takes string as separator? - c#

I have following string value: AnnualFee[[ContactNeeYear that I want to split using separator : "[["
The MSDN topic String.Split says that such function exists, so i used following code :
oMatch.Groups[0].Value.Split('[[');
but it throws an error saying:
Can not implicitly convert string[] to string
so how to split string value with separator : "[["

Try below code, it has worked for me:
string abc = "AnnualFee[[ContactNeeYear";
string[] separator = { "[[" };
string[] splitedValues = abc.Split(separator , StringSplitOptions.None);
I hope it will help you.. :):)

string text= "AnnualFee[[ContactNeeYear";
string[] parts= Regex.Split(text, #"\[\[");
The result is:
AnnualFee
ContactNeeYear
You can use Regex.Split(text, pattern) for such purpose.

In a short way ,
string yourString = "AnnualFee[[ContactNeeYear";
string [] _split = yourString.Split(new string[] { "[[" }, StringSplitOptions.None);

Related

How can i split a string with words separated by double comma?

String word = textBox1.Text;
string[] test = word.Split(",,");
If it's with one word.Split(","); it will work fine.
But in this case the string is in format: hello,,hi,,50,,70
And I want to parse it so in the array I will have:
hello hi 50 70
Getting error on: word.Split(",,");
Error 2 Too many characters in character literal
Error 3 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments
Error 4 Argument 1: cannot convert from 'string' to 'char[]'
Try this instead
string[] test = word.Split(new string[] { ",," }, StringSplitOptions.None);
I doubt if double ',' is a real deriver; probaly you have just empty fields. To skip these fields, use StringSplitOptions.RemoveEmptyEntries:
string source = "hello,,hi,,50,,70"
string[] result = source.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
you could also split it by 1 comma only, then remove the empty array
string[] test = word.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);

Convert text to string array with separator

I want to convert an string to string[] separate with ','.
string text = "1,2,5,6";
string[] textarray =?
Use this:
string text = "1,2,5,6";
string[] text_array =text.Split(new []{','});
To remove empty entries use:
string[] text_array =text.Split(new []{','}, StringSplitOptions.RemoveEmptyEntries);
string text = "1,2,5,6";
string[] textarray = text.split(',');
array=text.split(",");
it's simple enough.
Use below code.
string[] textArr = text.Split(",");

Remove html tags and split it when end tag was found in c#

I want to remove all html tags from the following string and split it without using the period(full stop) as matching character. The following sting is dynamic one which can have more conditions inside list tag
<li>This Offer cannot be redeemed with any other offer.</li><li>Only one Offer can be used at a time.</li><li>This Offer is not transferable.</li><li>......</li><li>....</li</ul></div>
I'm Expecting the following relult
This Offer cannot be redeemed with any other offer.
Only one Offer can be used at a time.
This Offer is not transferable.
....
....
String[] myString = yourString.replace("<li>", "").Split(new string[] { "</li>" }, StringSplitOptions.RemoveEmptyEntries);
try this
const string HTML_TAG_PATTERN = "<[^/li]>"; // may require some change
string safeString = Regex.Replace(yourString, HTML_TAG_PATTERN, string.Empty);
String[] myString = safeString.Split(new string[] { "</li>" }, StringSplitOptions.RemoveEmptyEntries);
you can try this regex too
string acceptable = "li";
string stringPattern = #"</?(?(?=" + acceptable + #")notag|[a-zA-Z0-9]+)(?:\s[a-zA-Z0-9\-]+=?(?:(["",']?).*?\1?)?)*\s*/?>";
string yourString= Regex.Replace(yourString, stringPattern, string.Empty);
String[] myString = yourString.replace("<li>", "").Split(new string[] { "</li>" }, StringSplitOptions.RemoveEmptyEntries);
you can remove all html tag and split also by below code
string HTML_TAG_PATTERN = "<.*?>";
string str = #"<li>This Offer cannot be redeemed with any other offer.</li><li>Only one Offer can be used at a time.</li><li>This Offer is not transferable.</li><li>......</li><li>....</li</ul></div>";
string[] stString = Regex.Replace(str.Replace("</li>", "#$#"), HTML_TAG_PATTERN, string.Empty).Split("#$#".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
If you are able to give id to your <li> then ,
You can try with javascript code something like following>>
var str=doccument.getElementById("liID").innerHTML;
This thing you can try on windows onload event or any specific according to your application.

Char Parsing newline (C#)

the problem is simple but I can't figure out a way to overcome this.My applciation recieve a string (stringID) which is a list of IDs, either separated ";"+new line or just newline like :
ID1;
ID2;
ID3;
or
ID1
ID2
ID3
What im trying is to get a table with all those IDs ;
I tried :
string[] tabID = stringID.Split(';', char.Parse(Environment.NewLine));
And
string[] tabID = stringID.Split(';', '\r\n'));
string[] tabID = stringID.Split(';','\n');
nothing worked, can anyone help me ? thanks a lot
use the class StringReader and its method ReadLine to read each line individually.
The newline property is a string that can be one or two characters long, so use strings when you split. Use the RemoveEmptyEntries option, otherwise you will get the empty strings that are between the semicolon and the newlines.
string[] tabID = stringID.Split(
new string[] { ";", Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries
);
This should work:
string [] split = words.Split(new Char [] {';','\n'});
There is an overload for Split which takes a char[] of many separators.
Try:
String test = "ID1;ID2;ID3;";
String[] testarr = test.Split(';');
Try this:
string[] tabID = stringID.Split(new Char[] { ';', '\n', '\r'},
StringSplitOptions.RemoveEmptyEntries);

split a string in to multiple strings

123\r\n456t\r\n789
How can i split the string above in to multiple strings based on the string text
.split has only over load that takes char :(
string.Split has supported an overload taking an array of string delimiters since .NET 2.0. For example:
string data = "123text456text789";
string[] delimiters = { "text" };
string[] pieces = data.Split(delimiters, StringSplitOptions.None);
use string.split("text"), hope it will help.
I believe you want to split 123, 456, 789 as you have \r\n after them.
Easiest way I see is
string textVal = "123\r\n456t\r\n789";
textVal = textVal.replace("\r", "").replace("\n",",");
string arrVal[] = textVal.split(',');
Now you have arrVal containing 123, 456, 789.
Happy coding
String.Split supports also an array of strings. In your case you can do:
string s = "123\r\n456t\r\n789";
string[] parts = s.Split(new string[] {"\r\n"}, StringSplitOptions.None);

Categories

Resources