Null Fields in String Array Converted to '0' [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi there I have bunch of string array and there may or may not be null or empty in fields (see below), how do I use the simplest codes to convert them to string '0'?
The goal is to turn the following array
string[] NullFields = {"101.002","A","12","","3","","B"};
to be like
string[] NullFields = {"101.002","A","12","0","3","0","B"};
Thank you very much.

How about a simple loop?
for (int i = 0; i < NullFields.Length; i++) {
if (string.IsNullOrEmpty(NullFields[i])
NullFields[i] = "0";
}

This is easy with LINQ:
string[] NullFields = {"101.002","A","12","","3","","B"}
.Select(x => string.IsNullOrEmpty(x) ? "0" : x)
.ToArray();

First of all "" != null in C# and there's a huge difference between the two - bedtime reading.
Second of all, here you go:
var myArray = NullFields.Select(s => String.IsNullOrEmpty(s) ? "0" : s)
.ToArray();

This will solve your problem.
string[] NullFields = { "101.002", "A", "12", "", "3", "", "B" };
for (int i = 0; i < NullFields.Length; i++)
{
if (NullFields[i] == null || NullFields[i] == "")
NullFields[i] = "0";
}
Hope it will help you !

Related

C# nicer way of writing if statement with two values [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I was wondering if anyone could please tell me a nicer way of writing this. These are the only conditions that are used.
private static void BondedNonBoundedIndicator(InvoiceLine invoiceLine, Product packageProduct)
{
var value = packageProduct.BondedQuantity;
var value2 = packageProduct.NonBondedQuantity;
if (value == 0 && value2 == 1)
{
invoiceLine.BondedORBbondedIndicator = "N";
}
if (value == 1 && value2 == 0)
{
invoiceLine.BondedORBbondedIndicator = "Y";
}
}
Using C# 8, we can use tuples and the switch expression to simplify this:
invoiceLine.BondedORBbondedIndicator = (value, value2) switch {
(0, 1) => "N",
(1, 0) => "Y",
_ => invoiceLine.BondedORBbondedIndicator
};
You could shorten the last case even further if you know the invoiceLine.BondedORBbondedIndicator before the code is run is always, say ""(, then the last case would be _ => "").
You can use xor logic
If(value1^value2 == 1)
value1 == 0 ? invoiceLine.BondedORBbondedIndicator = "N" : invoiceLine.BondedORBbondedIndicator = "Y";

How to Pass Next Level Alphabetical String using C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to pass if input string is "A" means i need to get next leve as "B". If input value is "B" means "C".
E.g:
string input1 = "A";
string input2 = "B";
output:
input1 ---> B
input2 ---> C
How to do it in programmatically. Please give me a solution to do this.
This is the best as far as i understand the problem:
public char NextLevel(char c)
{
if (c == 'Z')
return 'A';
return (char)(c + 1);
}
private string NextChar(string character)
{
if (character == null) throw new ArgumentNullException(nameof(character));
if (character.Length != 1) throw new ArgumentOutOfRangeException(nameof(character), "You can use only a single letter string");
return Convert.ToString(NextChar(character[0]));
}
private char NextChar(char character)
{
return (char)(character + 1);
}
If you need more details you can use this link: https://github.com/jenseralmeida/stackoverflow/blob/q37608162/src/LibraryTests/DraftTests.cs
This should work for you.
public string NextAlphaString(string x)
{
if(x == "z" || x == "Z")
return x.Replace(x[0],(char)(x[0] - 25));
else
return x.Replace(x()[0],(char)(x[0] + 1));
}

How do I make program with c# to check to see if a word is a palindrome? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
so i'm trying to make a program (text based) that can scan a word you type into it to see if it's a palindrome or not so just a true or false outcome i'm a beginner with c# so please help me out!
So far this is all i have done.
//Name space
namespace PalindromeChecker
{
//Class
class Program
{
//Function
static void Main(string[] args)
{
Console.WriteLine("Type the word");
ConsoleKeyInfo Keyinfo = Console.Readkey();
}
}
}
A palindrome is a sequence of characters that is the same whether read forward or in in reverse. So to determine if a string is a palindrome, we can compare the string with its reverse, it is one.
So how can you create a reverse of a string? If you look at the String definition, you'll see it implements IEnumerable. Now the System.Linq namespace offers the IEnumerable.Reverse method. This can be used to reverse a string:
var str = "oxo";
var reversed = str.Reverse();
However, this returns an enumeration of char, not a string. So string.Concat is needed to turn reversed into a string. Having done that, we now have:
var str = "oxo";
var reversed = string.Concat(str.Reverse());
To test if they are the same, simply compare them:
if (str == reversed)
{
// we have a palindrome
}
A "gotcha here is that if str is null, you'll get a null exception with str.Reverse. That can be handled with a null check. So we can then simplify the whole thing down to:
if (str != null && str == string.Concat(str.Reverse()))
{
// we have a palindrome
string word = "hi"; ;
char[] wordArray = word.ToCharArray();
char[] revWordArray = wordArray.Reverse().ToArray();
int count = 0;
for (int i1 = 0; i1 < wordArray.Length; i1++)
{
if (wordArray[i1] == revWordArray[i1])
{
count++;
}
}
if (count == wordArray.Length)
{
bool a = true;
}

How do I turn .Any into a for loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having some trouble turning the following code into a for loop. The purpose of the code is to check if the string has both a letter and a number in it.
else if (!string.Any(Char.IsLetter) || !string.Any(Char.IsDigit))
{
return false;
}
Any ideas?
Do you mean something like this?
bool anyLetter = false;
bool anyDigit = false;
foreach(var ch in str)
{
if(char.IsLetter(ch)) anyLetter = true;
if(char.IsDigit(ch)) anyDigit = true;
if(anyLetter && anyDigit) break;
}
return anyLetter || anyDigit;
Note that if this string should contain at least one digit and one letter, you need to use && instead of ||
Since Selman22 seems to have already answered the question, another solution I found is I guess you could also use RegEx:
letterCount = Regex.Matches(yourstring,#"[a-zA-Z]").Count;
numberCount = Regex.Matches(yourstring,#"\d").Count;
return letterCount != 0 && numberCount != 0;
Did you mean a loop for a set of strings?
var src = new List<string>{"A4C", "D3F", "G7I"};
var allHaveCharAndDigit = src.TrueForAll(s => s.Any(c => char.IsLetter(c)) && s.Any(c => char.IsDigit(c)));

Splitted words from string serialization [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I serialize a string like this: "Yes,No" into something like this {"0":"Yes", "1":"No"} ?
I have split the string by ',' and I've stored value into a KeyValuePair<int,string>. After serialization, the result is something like this:
{"Key" : "0" , "Value" : "Yes"},{"Key":"1","Value":"No"}
var result = yourString.Split(',').Select((v, k) => new { k, v, })
.ToDictionary(a => a.k, a => a.v);
You could always just serialize it yourself:
string theString = "True,False";
var result = theString.Split(',')
.Select((v, i) => string.Format("\"{0}\":\"{1}\"", i, v));
To get the final result you're asking for you could do this:
String.Join(",", input.Split(',')
.Select((s,i) => "{\"Key\":\""+i+"\",\"Value\":\""+s+"\"}"));
string s = "yes,no";
var d = s.Split(',');
var result = new Dictionary<int,string>(){};
for (int i = 0; i < d.Length; i++) {
result.Add(i, d[i]);
}

Categories

Resources