List<string> items to string [duplicate] - c#

This question already has answers here:
Convert a list to a string in C#
(14 answers)
Closed 4 years ago.
I wanted to ask if it's possible to change List items to one string?
List<string> example = new List<string>();
example.Add("1");
example.Add("2");
example.Add("3");
string text = "123";

string.Join is for you
string s = string.Join("", example )

Related

How do I replace a special character [duplicate]

This question already has answers here:
string.Replace (or other string modification) not working
(4 answers)
Closed 12 months ago.
I'm trying to replace a special character "½" with ".5"
cell.InnerText="o208½-105u208½-109o208-110u209-110";
string tempStr=cell.InnerText;
if (cell.InnerText.Contains("½"))
{
cell.InnerText.Replace("½", ".5");
}
string tempStr1 = cell.InnerText;
but my C# .Replace isn't working , I get the same result.
String is an immutable type. Compiler creates a new string after replacing. So try just this
var innerText = "o208½-105u208½-109o208-110u209-110";
innerText= innerText.Replace("½", ".5");
result
before - o208½-105u208½-109o208-110u209-110
after - o208.5-105u208.5-109o208-110u209-110

linq select from array name with value [duplicate]

This question already has answers here:
int array to string
(13 answers)
Closed 2 years ago.
Hi having array of ints like ages int[]{123, 234}
how to select it to string which can be used as get request ages=123&ages=234
You can use a combination of string.Join and Select:
string.Join("&", ages.Select(age => $"ages={age}"));
To do proper URL encoding, you can use HttpUtility.ParseQueryString() like this:
var query = System.Web.HttpUtility.ParseQueryString(string.Empty);
foreach (var age in ages)
{
query.Add("ages", age.ToString());
}
// or: ages.ToList().ForEach(age => query.Add("ages", age.ToString()));
return query.ToString();
However, this will lead to ages=123,234.

how to convert int variables to string variables [duplicate]

This question already has answers here:
Convert int to string?
(12 answers)
Closed 4 years ago.
I am currently splitting a string from a textbox that the user will fill with three numbers. Those numbers i want to be saved as seperate integers. Any help as to how to do this? Thanks for any help!
string[] count = txtPoemInput.Text.Split('/'); //Splitting values for keyword numbers
int Poem, Line, Word;
count[0] = Poem.ToString; // Example
count[1] = Line; // Example
count[2] = Word;
Here is what you need to do. Use Convert.ToInt32
Poem = Convert.ToInt32(count[0]);
Line = Convert.ToInt32(count[1]);
Word = Convert.ToInt32(count[2]);

Arraylist - Separate a string and put it into an array [duplicate]

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

Splitting string in c# by delimiter [duplicate]

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

Categories

Resources