C# Increment Alphabet character [duplicate] - c#

This question already has answers here:
How to find out next character alphabetically?
(8 answers)
Closed 5 years ago.
I'm trying to increment the first character of a string to the next letter in the Alphabet.
I have found this:
string str_A1 = "A1";
string str_B;
str_B= str[0]++;
Here str_B should be "B" but I get an error saying:
Property or indexer 'string.this[int]' cannot be assigned to -- it is read only

You cannot modify the value of the first character of str_A1, which is what the ++ operator is doing. Do this instead:
str_B = ((char)(str[0] + 1)).ToString();

You can do it something like:
string str_A1 = "A1";
string str_B = (char)(str_A1[0] + 1) + str_A1.Remove(0, 1);

Related

Remove strings till a char is recognized [duplicate]

This question already has answers here:
How to remove a defined part of a string?
(8 answers)
Closed 1 year ago.
I am trying to delete parts of the string (first 10 chars) so that I get the serial code of the string without any extra chars. Now, the serial code will always begin after the ":" colon char. So is there a way to specify to delete strings from ":" and before that so that only remaining string would be the serial key?
for example;
string is "MySerials:12e42-23w6z-23w-a23"
final string must be "12e42-23w6z-23w-a23"
I am deleting the strings manually;
public string myStr;
public void Start () {
myStr = myStr.Substring (10, myStr.Length - 10);
Debug.Log (myStr);
}
I would use the string split function like so:
var teststring = "MySerials:12e42-23w6z-23w-a23";
var split = teststring.Split(':');
Console.WriteLine(split[1]);
Instead of splitting the string you could look for the first occurrence of ':' and get your result directly:
var input = "MySerials:12e42-23w6z-23w-a23";
var result = input.Substring(input.IndexOf(':') + 1);

C# Check if any string contains in string array and get the value [duplicate]

This question already has answers here:
Find index of a value in an array
(8 answers)
Getting the index of a particular item in array
(5 answers)
Closed 2 years ago.
I am new in c# and stucked with getting value from arrays. I can check if the string contains any string in array but i have no idea how to get the value of matched string.
In my code i want to get "When is" as a string.
string testwords = "When is your birthday";
string[] myStrings = { "Who is ", "When is ", "What is " };
if(myStrings.Any(testwords.Contains))

c# replace character by an empty string [duplicate]

This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
I'm getting a string from a Json :
var value = JsonObject["price"]; //value = "1,560";
i'm trying to replace the ',' with an empty string :
value.Replace(",",string.Empty);
but i'm still getting the value with "," that's so strange and i'm stuck at it
thanks in advance
value = value.Replace( ", ", string.Empty);
strings in .net are immutable.
Per the documentation for String.Replace:
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
It gives you a new string; it doesn't modify the existing one. So you need to assign the result to a variable:
value = value.Replace(",", string.Empty);

string[0] get first character from string [duplicate]

This question already has answers here:
What does C# do when typecasting a letter to an int?
(5 answers)
Closed 6 years ago.
I have a problem which I do not understand. Here is my code:
String input = "3 days ago"
String firstCharacter = input[0].ToString(); //Returns 3
int firstCharacter = (int)input[0]; //Returns 51
Why does it return 51?
PS: My code comes from this thread: C#: how to get first char of a string?
More information:
In case that input = "5 days ago", then int firstCharacter is 53.
Casting a char to int in this way will give you its ASCII value which for 3 is equal to 51. You can find a full list here:
http://www.ascii-code.com/
You want to do something like this instead:
Char.GetNumericValue(input[0]);
You can also use substring to extract it as a string instead of a char and avoid having to cast it:
string input = "3 days ago";
string sFirstCharacter = input.Substring(0, 1);
int nFirstCharacter = int.Parse(input.Substring(0, 1));

string capturing before / symboal [duplicate]

This question already has answers here:
Get Substring - everything before certain char
(9 answers)
Closed 8 years ago.
Lets say I have a string:
string a = "abc&dcg / foo / oiu";
now i would like the output to be
"abc&dcg"
i have tried:
string output= a.Substring(a.IndexOf('/'));
but it returns the last part not the first part
I have tried trim() as well, but doesn't provide me with the results.
Try this:
string result = a.Split('/')[0].Trim();
The split operation will give you the 3 substrings separated by '/' and you can choose whichever ones you want by specifying the index.
Try this one
string a = "abc&dcg / foo / oiu";
string output = a.Substring(0, a.IndexOf("/"));
Console.WriteLine(output);
It will show
abc&dcg
Try
string output;
if (a.IndexOf('/')>=0) { output = a.Split('/')[0].Trim() };
This wil prevents error case a doesn't contains any /

Categories