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);
Related
I have been stuck in a situation .
Here are few input strings -
"abacuses\r\n25"
"alphabet\r\n56,\r\n57"
"animals\r\n44,\r\n45,\r\n47"
I need the output to be splited like -
"abacuses\r\n25" to be splitted into A)abacuses B)25
"alphabet\r\n56,\r\n57" to be splitted into A)alphabet B)56,57
"animals\r\n44,\r\n45,\r\n47" to be splitted into A)animals B)44,45,47
So far I have tried this but it doesn't work-
string[] ina = Regex.Split(indexname, #"\r\n\D+");
string[] ina = Regex.Split(indexname, #"\r\n\");
Please Help
No regex needed in your example. You basicaly parse string:
string input = "animals\r\n44,\r\n45,\r\n47";
var split = input.Split(new char[]{'\r','\n',','}, StringSplitOptions.RemoveEmptyEntries);
var name = split[0]; //animals
var args = string.Join(",", split.Skip(1)); //44,45,37
Many people use it for parsing, but Regex is not a parsing language! It is pattern matcher! It is used to find substrings in string! If you can just Split your string - just do it, really. It is much easier to understand than Regex expression.
If you need to split a string at the first \r\n, you may use a String.Split with a count argument:
var line = "animals\r\n44,\r\n45,\r\n47";
var res = line
.Split(new[] {"\r\n"}, 2, StringSplitOptions.RemoveEmptyEntries);
// Demo output
Console.WriteLine(res[0]);
if (res.GetLength(0) > 1)
Console.WriteLine(res[1].Replace("\r\n", "")); // In the second value, linebreaks should be removed
See the C# demo
The 2 in .Split(new[] {"\r\n"}, 2, StringSplitOptions.RemoveEmptyEntries) means that the whole string should be split into 2 parts only and since the string is processed from left to right, the split will occur on the first "\r\n" substring found.
I wonder if it's possible to use split to divide a string with several parts that are separated with a comma, like this:
10,12-JUL-16,11,0
I just want the Second part, the 12-JUL-16 of string and not the rest?
Yes:
var result = str.Split(',')[1];
OR:
var result = str.Split(',').Skip(1).FirstOrDefault();
OR (Better performance - takes only first three portions of the split):
var result = str.Split(new []{ ',' }, 3).Skip(1).FirstOrDefault();
Use LINQ's Skip() and First() or FirstOrDefault() if you are not sure there is a second item:
string s = "10,12-JUL-16,11,0";
string second = s.Split(',').Skip(1).First();
Or if you are absolutely sure there is a second item, you could use the array accessor:
string second = s.Split(',')[1];
Yes, you can:
string[] parts = str.Split(',');
Then your second part is in parts[1].
or:
string secondPart = str.Split(',')[1];
or with Linq:
string secondPart = str.Split(',').Skip(1).FirstOrDefault();
if (secondPart != null)
{
...
}
else
{
...
}
Also you can use not only one symbol for string splitting, i.e.:
string secondPart = str.Split(new[] {',', '.', ';'})[1];
You could use String.Split, it has an overloaded method which accepts max no of splits.
var input = "10,12-JUL-16,11,0"; // input string.
input.Split(new char[]{','},3)[1]
Check the Demo
Here's a way though the rest have already mentioned it.
string input = "10,12-JUL-16,11,0";
string[] parts = input.Split(',');
Console.WriteLine(parts[1]);
Output:
12-JUL-16
Demo
I have a string with the following text:
:0c4b7fcdffc38322555a9e35c22c9469:Nick:194176015020283762507:
How do I parse the final number? i.e.:
194176015020283762507
You should first use String.Split() to separate the string by the colon (':') separators. Then access the correct element.
var input = ":0c4b7fcdffc38322555a9e35c22c9469:Nick:194176015020283762507:";
var split = input.Split(':');
var final = split[3];
Note that by default, Split() keeps empty entries. You will have one at the beginning and end, because of the initial and ending colons. You could also use:
var split = input.Split(new[] {':'}, StringSplitOptions.RemoveEmptyEntries);
var final = split[2];
which, as the option implies, removes empty entries from the array. So your number would be at index 2 instead of 3.
string str = ":0c4b7fcdffc38322555a9e35c22c9469:Nick:194176015020283762507:";
string num = str.Split(':')[3];
var finalNumber = input.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)
.Last()
This code will split your input string into strings, separated by : (empty strings are removed from start and end of sequence). And last string is returned, which is your finalNumber.
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);
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;