Split string var - c#

Let's say I have this string var:
string strData = "1|2|3|4||a|b|c|d"
Then, I make a Split:
string[] strNumbers = strData.Split("||"); //something like this, I know It's not this simple
I need two separate parts, each one containing this:
//strNumbers -> {"1","2","3","4"},{"a","b","c","d"}
So that after that, I could do this:
string[] strNumArray = strNumbers[0].Split('|');
//strNumArray -> '1', '2', '3', '4'
And same with the other part (letters).
Is it possible? to make this double split with the same character, but the first time the character is repeated twice?.
Thanks.
PD. I'm using C#.

It'll work fine, your syntax is just off.
So first, your declarations are off. You want the [] on the type, not the name.
Second, on String.Split, there is an overload that takes in a string array and a StringSplitOptions. Just trying to do "||" will call the param char overload, which is invalid.
So try this:
string strData = "1|2|3|4||a|b|c|d";
string[] strNumbers = strData.Split(new[] {"||"}, StringSplitOptions.None);
string[] strNumArray = strNumbers[0].Split('|');
You can change the StringSplitOptions to RemoveEmptyEntries if you wanted.

in .net 3.5:
string strData = "1|2|3|4||a|b|c|d";
var s1 = strData.Split(new string[] { "||" }, StringSplitOptions.None);
var numbers = s1[0].Split('|');
var letters = s1[1].Split('|');

Dim s As String = "1|2|3|4|5|6|7||a|b|c|d|e|f"
Dim nums() As String = s.Split(New String() {"||"}, StringSplitOptions.None)(0).Split("|")
Dim strs() As String = s.Split(New String() {"||"}, StringSplitOptions.None)(1).Split("|")

Related

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 can I find a string between repeated strings in C#?

How can I find a string between repeated strings?
For example, if
string str = #"||AAA||BBB||CCC||";
how can I find all the strings(AAA, BBB, CCC) in order between repeated strings(||)?
Just use String.Split:
var str = #"||AAA||BBB||CCC||";
var splits = str.Split(new string[] {"||"}, StringSplitOptions.RemoveEmptyEntries);

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

Splitting string only it meets requirements in C#

I'm trying to split a song title into two strings- the artist and song.
I receive the original string like this: "artist - song".
Using this code, I split the string using '-' as the spliiter:
char[] splitter = { '-' };
string[] songInfo = new string[2];
songInfo = winAmp.Split(splitter);
This works fine and all, except when I get to a band with '-' in the name, like SR-71.
However, since the original strings are separated with a space then a - and a space again (like SR-71 - Tomorrow), how would I split the string so this happens? I tried changing splitter to a string and inputting
string[] splitter = { " - " };
in it, but it returns that there is no overload match.
For some reason, string.Split has no overload that only takes a string array.
You need to call this overload:
string[] songInfo = winAmp.Split(new string[] { " - " }, StringSplitOptions.None);
Don't ask me why.
You can also use
Match M = System.Text.RegularExpressions.Regex.match(str,"(.*?)\s-\s(.*)");
string Group = M.Groups[1].Value;
string Song = M.Groups[2].Value;

Categories

Resources