Split comma delimited string into smaller ones - .net-2.0

How would I split a comma delimited string into smaller comma delimited strings?
My string looks like this: 1,2,3,4,5,6,7,8,9,10
And I need to split the string after every nth occurrence of the , character.
E.g. for every 3rd occurrence, the above string would be turned into these strings:
1,2,3,4 5,6,7,8 9,10
Might look like homework but it's not, my brain is just tired but I still need to get work done.

Try a loop in which you count the commas ;-)
Untested, it could look like:
int lastSplit = 0;
int commaCount = 0;
int n = 4;
List<string> parts = new List<string>();
for (int i = 0; i < s.Length; i++)
{
if (s[i] == ',' && ++commaCount == n)
{
commaCount = 0;
parts.Add(s.Substring(lastSplit, i - lastSplit));
lastSplit = i + 1;
}
}
parts.Add(s.Substring(lastSplit));

You could do it via regex. Try out ((?:[^,]+)(?:,(?:(?:[^,]*))){0,3}) on rubular
Oh, and then you just need to swap out the "3" in the regex for whatever number of commas you need.

So?
[TestMethod]
public void test()
{
string text = "1,2,3,4,5,6,7,8,9,10";
var lists = Regex.Matches(text, ".,.,.,.");
foreach (var x in lists)
{
Console.WriteLine(x.ToString());
}
}

Related

how to replace special characters in a string by "_"

hello i have a string ch and i want to replace the special characters if it exist by "_"
how can detect all special characters is there another way .
can we use replaceAll and include all the special characters
for(int i=0; i<s.length; i++) {
var char = s[i];
if (char = "/" || char ="." || char ="$")
{
s[i] ="_"
}
}
You can do it like this with regex
_string.replaceAll(RegExp('[^A-Za-z0-9]'), '_');
This will replace all characters except alphabets and numbers with _
The best way to do this would be to use RegEx inside a .replaceAll() method.
In your case you can use a regex that matches all character other than English Alphabets..
String result = s.replaceAll(RegExp('[^A-Za-z]'), '_');
Or you can be more specific and create a regex of your requirement

flutter: change strings letters from the letters from list 1 to the letters from list 2

I am trying to change a string letters from the letters from list 1 to the letters from list 2
and I couldn't find a way to do it
this is my 2 lists
List En = ["A","A","B","G","D","R","S","C","T","E","F","K","L","M","N","H","W","Y","Y"];
List Ar = ["ا","أ","ب","ج","د","ر","س","ص","ط","ع","ف","ق","ل","م","ن","ه","و","ى","ي"];
so if the string was "abc" for example it would get the equivalent of the chars A B C from list 1 and then translate them to the same indexes in list 2
I don't know why others are happy with linear searches of lists. To me, that screams for setting up a map one time, and using it repeatedly. Here's what I whipped up in DartPad:
void main() {
var En = ['a', 'b', 'c'];
var Ar = ['1', '2', '3'];
var en2ar = Map<String, String>.fromIterables(En, Ar);
print(en2ar);
var text = 'abcd';
var output =
text.replaceAllMapped(RegExp('.'), (Match m) => en2ar[m.group(0)] ?? '');
print('$text => $output');
}
you can use this function:
setText(){
String input = 'ABC';
List text = input.split('');
String output = '';
List En = ["A","A","B","G","D","R","S","C","T","E","F","K","L","M","N","H","W","Y","Y"];
List Ar = ["ا","أ","ب","ج","د","ر","س","ص","ط","ع","ف","ق","ل","م","ن","ه","و","ى","ي"];
text.forEach((item){
int index = En.indexWhere((element) => element == item);
if(index != -1){
output = output + Ar[index];
}
});
return output;
}
using the indexWhere method you can find the index of a certain char in array a then replace the char with the element at the same index in array b
I would use this flow
cycle the characters of the string you want to repalce
for every character you get the index of the array a
add the item at the same index in the array b to a "result" string
You should now have a string "result" with the characters replaced

How to use split method with string containing brackets?

I have a string that contains some data. Data is separated like this:
var stringData = (SomeWordsWithSpacesInBetween) 0 (SomeWordsWithSpaceInBetween) 1 ...
I want to be able to extract data between the brackets and numbers between the words in brackets as such:
stringData.split( some way to split them)[0] = SomeWordsWithSpacesInBetween;
stringData.split(some way to split them)[1] = 0;
How to split them this way?
var s = '(Some Words With Spaces InBetween) 0 (SomeWordsWithSpaceInBetween) 1';
var r = RegExp(r'\(((\w+ ?)*)\) (\d+) ?').allMatches(s).expand((e) => [e[1], e[3]]);
You can do it using regular expression. Here is an example.
List<String>getStringList(){
String abc = '(SomeWordsWithSpacesInBetween) 0 (SomeWordsWithSpaceInBetween) 1 (SomeWordsWithSpaceInBetween)';
List<String> myList = new List();
RegExp exp = new RegExp(r"\) (\d+) \(");
myList = abc.split(exp);
print('${myList}');
return myList;
}

Shifting a string in matlab

Ok so I have retrieved this string from the text file now I am supposed to shift it by a specified amount. so for example, if the string I retrieved was
To be, or not to be
That is the question
and the shift number was 5 then the output should be
stionTo be, or not
to beThat is the que
I was going to use circshift but the given string wouldn't of a matching dimesions. Also the string i would retrieve would be from .txt file.
So here is the code i used
S = sprintf('To be, or not to be\nThat is the question')
circshift(S,5,2)
but the output is
stionTo be, or not to be
That is the que
but i need
stionTo be, or not
to beThat is the que
By storing the locations of the new lines, removing the new lines and adding them back in later we can achieve this. This code does rely on the insertAfter function which is only available in MATLAB 2016b and later.
S = sprintf('To be, or not to be\nThat is the \n question');
newline = regexp(S,'\n');
S(newline) = '';
S = circshift(S,5,2);
for ii = 1:numel(newline)
S = insertAfter(S,newline(ii)-numel(newline)+ii,'\n');
end
S = sprintf(S);
You can do this by performing a circular shift on the indices of the non-newline characters. (The code below actually skips all control characters with ASCII code < 32.)
function T = strshift(S, k)
T = S;
c = find(S >= ' '); % shift only printable characters (ascii code >= 32)
T(c) = T(circshift(c, k, 2));
end
Sample run:
>> S = sprintf('To be, or not to be\nThat is the question')
S = To be, or not to be
That is the question
>> r = strshift(S, 5)
r = stionTo be, or not
to beThat is the que
If you want to skip only the newline characters, just change to
c = find(S != 10);

what is the substr? in the systemverilog

Int fd;
String str;
fd = $fopen(path, "r");
Status= $fgets(str, fd);
cm = str.substr(0,1);
cm1= str.substr(0,0);
I want to know what is substr function? What is the purpose above that??
The substr function returns a new string that is a substring formed by characters in position i through j of str. Very similar to examples posted here.
module test;
string str = "Test";
initial
$display(str.substr(0,1));
endmodule
The output will be:
>> Te
As you can see in section 6.16.8, IEEE SystemVerilog Standard 1800-2012.
substr function, as it name suggests, subtracts, or takes a chunk from a bigger string, in systemverilog.
Example:
stri0 = "my_lago";
stri1 = stri0.substr(1,5);
$display("This will give stri1 = %s" , stri1);
....
OUTPUT :- This will give stri1 = y_lag
Substring: This method extracts strings. It needs the Position of the substring ( start index, length). It then returns a new string with the characters in that range.
C# program Substring
using System;
class Program
{
static void Main()
{
string input = "ManCatDog";
// Get Middle three characters.
string subString = input.Substring(3, 6);
Console.WriteLine("SubString: {0}", subString);
}
}
Output
Substring: Cat