This question already has answers here:
How to convert string[] to ArrayList?
(4 answers)
Closed 7 years ago.
I have a string "foo|bar|time||||etc|"
I want to loop through each entry and save them one by one in a ArrayList. The delimiter is '|'.
For example, the ArrayList should contain:
"foo"
"bar"
"date"
""
""
""
"etc"
""
How do I save each string in an ArrayList?
You can easily Split your string with | and use ToList method like;
var s = "foo|bar|time||||etc|";
var list = s.Split('|').ToList();
And it's almost 2016. Don't use ArrayList anymore. This structure belongs on old days when C# doesn't have Generics. Use List<T> instead.
use string.Split and ToList
string str = "foo|bar|time||||etc|";
List<string> words = str.Split('|').ToList(); //a lot better than ArrayList
Related
This question already has answers here:
Put result of String.Split() into ArrayList or Stack
(5 answers)
Closed 2 years ago.
I was wondering if there is a way to convert example:
'Hello.World' into '["Hello"]["World"]' or 'This.is.a.string' into ["This"]["is"]["a"]["string"]
I'm kinda new to C# and I was wondering if that's even possible using string formatting or something like that.
You can use the Split method like this string[] strings = String.Split("."); This will split your string per each period.
If you meant to include the single quotes (') as part of the string then:
String test = "'Hello.World'";
// Strip the first and last '
test = test.Substring(1, test.Length - 2);
// Split on Period
String[] split = test.Split('.');
// Encapsulate each word with [" "]
// and add back in the single quotes
var result = $"'{String.Join("", split.Select(word => $"[\"{word}\"]"))}'";
Prints:
'["Hello"]["World"]'
If they just meant to surround your input then just:
String test = "Hello.World";
// Split on Period
String[] split = test.Split('.');
// Encapsulate each word with [" "]
var result = $"{String.Join("", split.Select(word => $"[\"{word}\"]"))}";
Prints: ["Hello"]["World"]
This question already has answers here:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
Closed 5 years ago.
I want to separate a string and put it into an array.
Example:
id = 12,32,544,877,136,987
arraylist: [0]-->12
[1]-->32
[2]--544
[3]-->877
[4]-->136
[5]-->987
How to do that?
If your id var is a String, you can use the Split method:
id.Split(',')
Try:
string[] arraylist = id.Split(',');
Can do something like this in java :
ArrayList<String> idList = new ArrayList <String>();
String id = "12,32,544,877,136,987";
String idArr[] = id.split(",");
for(String idVal: idArr){
idList.add(idVal);
}
This question already has answers here:
Split String in C#
(9 answers)
Closed 7 years ago.
I've a string like this
cscript "E:\Data\System Test Performance\Some Tool\script.vbs" "AP_TEST" %%PARM1
I'm splitting above string like below
cmd.Split(' ')
Expected:
cscript
"E:\Data\System Test Performance\Some Tool\script.vbs"
"AP_TEST"
%%PARM1
But Actual results
There is a space in the string so your result is as expected. Try splitting on the quote instead:
var str = #"cscript ""E:\Data\System Test Performance\Some Tool\script.vbs"" ""AP_TEST"" %%PARM1";
str.Split('"').Select (s => s.Trim()).Where (s => !string.IsNullOrEmpty(s));
You need to write your own split function which supports text qualifiers
Check answer here Split String in C#
Or this article http://www.codeproject.com/Articles/15361/Split-Function-that-Supports-Text-Qualifiers
This might do the trick for you
string[] newinp = Regex.Split(inp, "(?=\")").Where(x => !string.IsNullOrEmpty(x)).ToArray();
There are so many spaces in your E:\Data\System Test Performance\Some Tool\script.vbs (file location) thats why you're getting the wrong array.
You may do two things
1) Make directory which doesn't contains spaces
2) Modify code
string[] final=new string[4];
final[0]=cmdLinesplit[0];
final[2]=cmdLinesplit[cmdLinesplit.Length-2];
final[3]=cmdLinesplit[cmdLinesplit.Length-1];
for(int i=1;i< cmdLinesplit.Length-2;i++)
{
final[1] +=cmdLinesplit[i]+" ";
}
final[1].Trim();
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# how to convert File.ReadLines into string array?
I have a text file that I want to make a String array from. The file is newline delimited. For example,
entry1
entry2
entry3
will make an array of {"entry1", "entry2", "entry3"}
EDIT: I am wanting to do this using DownloadString in WebClient
So you're just trying to split a string into an array that is delimited with a new line?
If so, this should work:
string temp = webClient.DownloadString(someurl);
string[] lines = temp.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
Good luck.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Split string, convert ToList<int>() in one line
Convert comma separated string of ints to int array
I have a string like:
string test = "1,2,3,4";
Is there any easier way (syntactically) to convert it to a List<int> equivalent to something like this:
string[] testsplit = test.Split(',');
List<int> intTest = new List<int>();
foreach(string s in testsplit)
intTest.Add(int.Parse(s));
You can throw LINQ at it:
List<int> intTest = test.Split(',').Select(int.Parse).ToList();
It first splits the string, then parses each part(returning an IEnumerable<int>) and finally constructs a list from the integer sequence.
var result = test.Split(',').Select(x => int.Parse(x));
Or, if you really want a List<int> (rather than just any IEnumerable<int>), append a .ToList().
test.Split(',').Select(x => int.Parse(x)).ToList()
Linq can make it a bit cleaner:
var intTest = test.Split(',').Select(s=>int.Parse(s)).ToList();