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);
Related
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);
This question already has answers here:
Text Split with Space
(2 answers)
Closed 5 years ago.
So I have a string input like this:
string pianist = "Johann Sebastian Bach";
How to split those by space so I can access such as:
pianist[0] == "Johann"
pianist[1] == "Sebastian"
pianist[2] == "Bach"
I tried
string test = pianist.Split(' ');
but its not working.
you can try this one;
string[] pianist = "Johann Sebastian Bach".Split(' ');
you will get as expected.
string test = pianist.Split(' '); // Doesn't work Why?
Because the .Split() method of String class Splits a string into substrings that are based on the characters in an array. you cannot assign them to a string, you should use an array instead.
What to do to make your code work?
Change the type of to string[] to hold the result of split:
string[] splitResult = pianist.Split(' ');
The problem is that your expectation is wrong.
so I can access such as pianist[0] == "Johann"
since pianist is your original string, trying to access it with an index will result in chars at this position in the string. (since a string is represented by a char[]).
If you look at the documentation of the method Split() you will see that it returns a string[] and not a string as you tried. You need first to catch this return value in an extra variable, then you can access it the way you planed:
string pianist = "Johann Sebastian Bach";
string [] returnedArray = pianist.Split(' ');
string johann = returnedArray[0];
string sabastian = returnedArray[1];
string bach = returnedArray[2];
You don't need to pass any params of your split method. check interested MSDN post:
If the separator argument is null or contains
no characters, the method treats white-space characters as the
delimiters. White-space characters are defined by the Unicode
standard they return true if they are passed to the Char.IsWhiteSpace
method.
String.Split Method
string pianist = "Johann Sebastian Bach";
var pianistArray = pianist.Split();
Result:
pianistArray[0] == "Johann"
pianistArray[1] == "Sebastian"
pianistArray[2] == "Bach
You are trying to save a string-array under a string variable. Try this:
string test = "Johann Sebastian Bach";
string[] separated = test.Split(' ');
foreach(string sub in separated)
{
Console.WriteLine(sub);
}
produces
Johann
Sebastian
Bach
Split function must specify the value to split. your situation is a space " ", not "".
here is the answer
string pianist = "Johann Sebastian Bach";
string[] asd = pianist.Split(" ");
It should see below looks like you have not given the space correctly
var res = pianist.Split(new[] {' '});
string test = pianist.Split(' ');
This won't work as the result from Split is an array. Do this instead:
string[] test = pianist.Split(' ');
No you can access the words like you already tried to:
test[0]
test[1]
...
Try this:
string pianist = "Johann Sebastian Bach";
var result = pianist.Split(' ');
Or:
string pianist = "Johann Sebastian Bach";
string[] result = pianist.Split(' ');
I want to split a string into 2 strings,
my string looks like:
HAMAN*3 whitespaces*409991
I want to have two strings the first with 'HAMAN' and the second should contain '409991'
PROBLEM: My second string has '09991' instead of '409991' after implementing this code:
string str = "HAMAN 409991 ";
string[] result = Regex.Split(str, #"\s4");
foreach (var item in result)
{
MessageBox.Show(item.ToString());
}
CURRENT SOLUTION with PROBLEM:
Split my original string when I find whitespace followed by the number 4. The character '4' is missing in the second string.
PREFERED SOLUTION:
To split when I find 3 whitespaces followed by the digit 4. And have '4' mentioned in my second string.
Try this
string[] result = Regex.Split(str, #"\s{3,}(?=4)");
Here is the Demo
Positive lookahead is your friend:
Regex.Split(str, #"\s+(?=4)");
Or you could not use Regex:
var str = "HAMAN 409991 ";
var result = str.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
EXAMPLE
Alternative if you need it to start with SPACESPACESPACE4:
var str = new[] {
"HAMAN 409991 ",
"HAMAN 409991",
"HAMAN 509991"
};
foreach (var s in str)
{
var result = s.Trim()
.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries)
.Select(a => a.Trim())
.ToList();
if (result.Count != 2 || !result[1].StartsWith("4"))
continue;
Console.WriteLine("{0}, {1}", result[0], result[1]);
}
That's because you're splitting including the 4. If you want to split by three-consecutive-spaces then you should specify exactly that:
string[] result = Regex.Split(str, #"\s{3}");
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(",");
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("|")