split a string in to multiple strings - c#

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);

Related

C# code to find a string between two strings - same identical strings

C# code to get the string between two strings
example :
mystring = "aaa.xxx.b.ccc.12345"
Need c# code to get the second string "xxx" between two ".", always the second string ignore other strings between the "." What is the best way to get "xxx" out of "aaa.xxx.b.ccc.12345"
And the second set of string can be anything
eg:
"aaa.123.b.ccc.12345" "aaa.re.b.ccc.45" "eee.stt.b.ccc.ttt" "233.y.b.ccc.5"
We can use string.Split() get an array of all strings delimited by the parameter you pass it. For example:
var strings = mystring.Split('.');
// strings = {"aaa", "xxx", "b", "ccc", "12345"}
var str = strings[1];
// str = "xxx"
mystring.Split('.').Skip(1).FirstOrDefault();
We split at each '.' and we ignore the first then take the first one.
We need handling of nulls. if not just use First
You can you this:
string[] mystrings = mystring.Split('.');
string secondString = strings[1];

Separate a string and place in array

How can I split this string and place in into an array can it be by a break line or new lines
This is the api url
https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/boxscoreplayertrackv2
I tried using but the string place it in index 0
string[] lines = split.Split(
new[] { Environment.NewLine },
StringSplitOptions.None
);
I would like to get each individual link from the string and store in an array
Source link: any-api.com
You can also call Split() without any arguments, in which case it will split on whitespace characters, so your sample code can simply be reduced to:
string[] lines = split.Split();
var array = urlString.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
This ensures there are no blank elements in array if URLs are separated by multiple spaces.
Here is a demo for you, you can try it.
string urlString = "https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/boxscoreplayertrackv2";
var array = urlString.Split(' ');
In your case, you can easily use the Regex like this:
string data = " https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/boxscoreplayertrackv2";
string regex = #"\s";
string[] result = Regex.Split(data.Trim(), regex);

How to separate a string into different substrings without known format

I am having trouble with this.
I have couple of strings like this [44, 74, 57]
Now I want to separate into 3 strings and omit the brackets and commas in C#, the output would be like this:
44
74
57
I have tried substring, concate, Regular expression but the problem is the strings, they don't have the same number of digits or format [xx, yy, zz]
Some strings are like [44, 74, 57], others are like [42, 6653, 40000]
Can anyone help me what should I use?
As long as the []'s and the ,'s are strongly consistent the number of digits will be irrelevant and Split should work:
string tempstr = "[44, 74, 57]";
string[] outarray = tempstr.Split("[,] ".ToArray(),StringSplitOptions.RemoveEmptyEntries);
the string in your questions appears to be a JSON string. if this is the case then
use json deserialiser instead. One good options is Newtonsoft.Json
and you can have the numbers as an int array by using the command
var array JsonConvert.DesrializeObject<int[]>("[1,2,3]");
string s = "[22, 4564, 65]";
string result[] = s.Replace('[', '').Replace(']', '').Split(',');
brute force regex /\[(\d)+, ?(\d+), ?(\d+)\]/ data will be captured in groups 1, 2 and 3 cc: http://rubular.com/r/eheSoJFJ8q
Or using replace and split
char[] delimiterChars = { ', ', ',' };
string text = "[1, 2,333]";
string[] words = text.Replace('[','').Replace(']','').Split(comma);
System.Console.WriteLine("just the numbers {0}:", words);
String s = "[11,12,23]";
String[] s1 = s.Substring(1, s.Length - 2).Split(',');
s1 will have 3 arrays containing three numbers.
string tempstr = "[44, 74, 57]";
tempstr=tempstr.Remove(']').Remove('[');
string[] outarray =tempstr.Split(",");
Try to remove the brackets
Then split the string by comma.
You will end up with an array of the values you were expecting.
String value=[44,74,57,444,555];
Value=value.replace("[","");
Value=value.replace("]","");
String[] allValues=value.split(new char[]{','});
Now the array should contain all the values from your string.
That is mostly pseudo code. I wrote this on a tablet.
Hope it helps.

String.Split with a String?

I have what is probably a very simple question.
I want to do a classic String.Split(), but with a string, not a character. Like string.Split("word") and get back an array just like if I had done string.Split('x').
You can use String.Split(string[], StringSplitOptions options).
The code will look like:
var results = theString.Split(new[] {"word"}, StringSplitOptions.None);
There is an available function overload on string.Split but it takes an array and an enum.
string test = "1Test2";
string[] results = test.Split(new string[] { "Test" }, StringSplitOptions.None);
Will yield an array containing "1" and "2".

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);

Categories

Resources