Convert String To Nullable Integer List - c#-4.0

I'm wanting to parse a string into a nullable int list in C#
I'm able to convert it to int list bit not a nullable one
string data = "1,2";
List<int> TagIds = data.Split(',').Select(int.Parse).ToList();
say when data will be empty i want to handle that part!
Thanks

You can use following extension method:
public static int? TryGetInt32(this string item)
{
int i;
bool success = int.TryParse(item, out i);
return success ? (int?)i : (int?)null;
}
Then it's simple:
List<int?> TagIds = data.Split(',')
.Select(s => s.TryGetInt32())
.ToList();
I use that extension method always in LINQ queries if the format can be invalid, it's better than using a local variable and int.TryParse (E. Lippert gave an example, follow link).
Apart from that it may be better to use data.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries) instead which omits empty strings in the first place.

Related

Understanding Graph, Weighted method

Okay, so what does the SET stand for in the second line? Why is the second string in<>, ?
public Weighted(In in, String delimiter) {
st = new ST<String, SET<String>>();
while (!in.isEmpty()) {
String line = in.readLine();
String[] names = line.split(delimiter);
for (int i = 1; i < names.length; i++) {
addEdge(names[0], names[i]);
}
}
}
With the little information you gave, I will assume that SET is an abstract data type. An abstract data type can store any values without any particular order and with no duplicates. By telling <String> after SET you are telling you want to store Strings inside your SET.
You can learn more about SETs here: https://en.wikipedia.org/wiki/Set_(abstract_data_type)

How to collect a string to a stack of characters in Java 8? [duplicate]

I would like to convert the string containing abc to a list of characters and a hashset of characters. How can I do that in Java ?
List<Character> charList = new ArrayList<Character>("abc".toCharArray());
In Java8 you can use streams I suppose.
List of Character objects:
List<Character> chars = str.chars()
.mapToObj(e->(char)e).collect(Collectors.toList());
And set could be obtained in a similar way:
Set<Character> charsSet = str.chars()
.mapToObj(e->(char)e).collect(Collectors.toSet());
You will have to either use a loop, or create a collection wrapper like Arrays.asList which works on primitive char arrays (or directly on strings).
List<Character> list = new ArrayList<Character>();
Set<Character> unique = new HashSet<Character>();
for(char c : "abc".toCharArray()) {
list.add(c);
unique.add(c);
}
Here is an Arrays.asList like wrapper for strings:
public List<Character> asList(final String string) {
return new AbstractList<Character>() {
public int size() { return string.length(); }
public Character get(int index) { return string.charAt(index); }
};
}
This one is an immutable list, though. If you want a mutable list, use this with a char[]:
public List<Character> asList(final char[] string) {
return new AbstractList<Character>() {
public int size() { return string.length; }
public Character get(int index) { return string[index]; }
public Character set(int index, Character newVal) {
char old = string[index];
string[index] = newVal;
return old;
}
};
}
Analogous to this you can implement this for the other primitive types.
Note that using this normally is not recommended, since for every access you
would do a boxing and unboxing operation.
The Guava library contains similar List wrapper methods for several primitive array classes, like Chars.asList, and a wrapper for String in Lists.charactersOf(String).
The lack of a good way to convert between a primitive array and a collection of its corresponding wrapper type is solved by some third party libraries. Guava, a very common one, has a convenience method to do the conversion:
List<Character> characterList = Chars.asList("abc".toCharArray());
Set<Character> characterSet = new HashSet<Character>(characterList);
Use a Java 8 Stream.
myString.chars().mapToObj(i -> (char) i).collect(Collectors.toList());
Breakdown:
myString
.chars() // Convert to an IntStream
.mapToObj(i -> (char) i) // Convert int to char, which gets boxed to Character
.collect(Collectors.toList()); // Collect in a List<Character>
(I have absolutely no idea why String#chars() returns an IntStream.)
The most straightforward way is to use a for loop to add elements to a new List:
String abc = "abc";
List<Character> charList = new ArrayList<Character>();
for (char c : abc.toCharArray()) {
charList.add(c);
}
Similarly, for a Set:
String abc = "abc";
Set<Character> charSet = new HashSet<Character>();
for (char c : abc.toCharArray()) {
charSet.add(c);
}
List<String> result = Arrays.asList("abc".split(""));
Create an empty list of Character and then make a loop to get every character from the array and put them in the list one by one.
List<Character> characterList = new ArrayList<Character>();
char arrayChar[] = abc.toCharArray();
for (char aChar : arrayChar)
{
characterList.add(aChar); // autoboxing
}
You can do this without boxing if you use Eclipse Collections:
CharAdapter abc = Strings.asChars("abc");
CharList list = abc.toList();
CharSet set = abc.toSet();
CharBag bag = abc.toBag();
Because CharAdapter is an ImmutableCharList, calling collect on it will return an ImmutableList.
ImmutableList<Character> immutableList = abc.collect(Character::valueOf);
If you want to return a boxed List, Set or Bag of Character, the following will work:
LazyIterable<Character> lazyIterable = abc.asLazy().collect(Character::valueOf);
List<Character> list = lazyIterable.toList();
Set<Character> set = lazyIterable.toSet();
Bag<Character> set = lazyIterable.toBag();
Note: I am a committer for Eclipse Collections.
IntStream can be used to access each character and add them to the list.
String str = "abc";
List<Character> charList = new ArrayList<>();
IntStream.range(0,str.length()).forEach(i -> charList.add(str.charAt(i)));
Using Java 8 - Stream Funtion:
Converting A String into Character List:
ArrayList<Character> characterList = givenStringVariable
.chars()
.mapToObj(c-> (char)c)
.collect(collectors.toList());
Converting A Character List into String:
String givenStringVariable = characterList
.stream()
.map(String::valueOf)
.collect(Collectors.joining())
To get a list of Characters / Strings -
List<String> stringsOfCharacters = string.chars().
mapToObj(i -> (char)i).
map(c -> c.toString()).
collect(Collectors.toList());

Add comma sequentially to string in C#

I have a string.
string str = "TTFTTFFTTTTF";
How can I break this string and add character ","?
result should be- TTF,TTF,FTT,TTF
You could use String.Join after you've grouped by 3-chars:
var groups = str.Select((c, ix) => new { Char = c, Index = ix })
.GroupBy(x => x.Index / 3)
.Select(g => String.Concat(g.Select(x => x.Char)));
string result = string.Join(",", groups);
Since you're new to programming. That's a LINQ query so you need to add using System.Linq to the top of your code file.
The Select extension method creates an anonymous type containing the char and the index of each char.
GroupBy groups them by the result of index / 3 which is an integer division that truncates decimal places. That's why you create groups of three.
String.Concat creates a string from the 3 characters.
String.Join concatenates them and inserts a comma delimiter between each.
Here is a really simple solution using StringBuilder
var stringBuilder = new StringBuilder();
for (int i = 0; i < str.Length; i += 3)
{
stringBuilder.AppendFormat("{0},", str.Substring(i, 3));
}
stringBuilder.Length -= 1;
str = stringBuilder.ToString();
I'm not sure if the following is better.
stringBuilder.Append(str.Substring(i, 3)).Append(',');
I would suggest to avoid LINQ in this case as it will perform a lot more operations and this is a fairly simple task.
You can use insert
Insert places one string into another. This forms a new string in your C# program. We use the string Insert method to place one string in the middle of another one—or at any other position.
Tip 1:
We can insert one string at any index into another. IndexOf can return a suitable index.
Tip 2:
Insert can be used to concatenate strings. But this is less efficient—concat, as with + is faster.
for(int i=3;i<=str.Length - 1;i+=4)
{
str=str.Insert(i,",");
}

Just Difference in C#

What is the differ between string.Join & string.Concat
similarly what is the diff between string.Equals & string.Compare
Show me with some example for each. I already searched but didn't understand.
Thanks in Advance.
Join combines several strings with a separator in between; this is most often used if you have a list and want to format it in a way that there is a separator (e.g. a comma) between each element. Concat just appends them all after another. In a way, Join with an empty separator is equivalent to Concat.
Equals determines whether two strings are considered equal, Compare is for determining a sort order between two strings.
Honestly, though, this is all explained very well in the documentation.
With .NET 4.0, String.Join() uses StringBuilder class internally so it is more efficient.
Whereas String.Concat() uses basic concatenation of String using "+" which is of course not an efficient approach as String is immutable.
I compared String.Join() in .NET 2.0 framework where its implementation was different(it wasn't using StringBuilder in .NET 2.0). But with .NET 4.0, String.Join() is using StringBuilder() internally so its like easy wrapper on top of StringBuilder() for string concatenation.
Microsoft even recommends using StringBuilder class for any string concatenation.
Program that joins strings [C#]
using System;
class Program
{
static void Main()
{
string[] arr = { "one", "two", "three" };
// "string" can be lowercase, or...
Console.WriteLine(string.Join(",", arr));
// ... "String" can be uppercase:
Console.WriteLine(String.Join(",", arr));
}
}
Output -
one,two,three
one,two,three
Concat:
using System;
class Program
{
static void Main()
{
// 1.
// New string called s1.
string s1 = "string2";
// 2.
// Add another string to the start.
string s2 = "string1" + s1;
// 3.
// Write to console.
Console.WriteLine(s2);
}
}
Output -
string1string2
these two methods are quite related. Although it hasn't been done, equals could have been implemented using compareTo:
public boolean equals(Object o)
{
if (this == anObject)
{
return true;
}
if (o instanceof String)
{
String s = (String)o;
return compareTo(s) == 0;
}
return false;
}
Also, s1.equals(s2) == true implies s1.compareTo(s2) == 0 (and vice versa), and s1.equals(s2) == false implies s1.compareTo(s2) != 0 (and vice versa).
However, and this is important, this does not have to be the case for all classes. It is for String, but no rule prohibits different natural orders for other classes.

Where predicate and Expression<Func<T, bool>>

I have this line of code that returns index of particular object in a IList<T>
int index = list.IndexOf(list.Where(x => x.Code == searchValue).FirstOrDefault());
and I have similar construction on many places, which searches collections on different properties. My goal is to automate this, so I can have a generic method MyClass<T>
int index = myClass.Find<T>(x=> x.Code == searchValue);
or
int index = MyClass.Find<T>(x => x.Name.ToUpper().StartsWith(searchValue.ToUpper()));
Is this possible with Lambda expressions?
Edit:
For anyone that is asking the same, here is the code that is working:
public int Find(Func<T, bool> whereClause)
{
return _list.IndexOf(_list.Where<T>(whereClause).FirstOrDefault<T>());
}
I'm not sure why you think you need to use an expression tree. Assuming list is a List<T>, you should be able to use FindIndex:
int index = list.FindIndex(x => x.Code == searchValue);
If that's not what you need, please give us more information about what the types involved are.

Resources