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);
Related
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);
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);
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 /
This question already has answers here:
Convert comma separated string of ints to int array
(9 answers)
Closed 8 years ago.
I have a string in C#. It's blank at the beginning, but eventually it will become something like
public string info12 = "0, 50, 120, 10";
One of you might be thinking, eh? Isn't than an integer array? Well it needs to stay a string for the time being, it must be a string.
How can I convert this string into an string array (Variable info13) so I can eventually reference it into more variables.
info 14 = info13[0];
info 15 = info13[1];
PLEASE NOTE: This is not a duplicate question. If you read the whole thing, I clearly stated that I have an array of strings not integers.
Here are a few options:
1. String.Split with char and String.Trim
Use string.Split and then trim the results to remove extra spaces.
public string[] info13 = info12.Split(',').Select(str => str.Trim()).ToArray();
Remember that Select needs using System.Linq;
2. String.Split with char array
No need for trim, although this method is not my favorite
public string[] info13 = info12.Split(new string[] { ", " }, StringSplitOptions.None);
3. Regex
public string[] info13 = Regex.Split(info12, ", ");
Which requires using System.Text.RegularExpressions;
EDIT: Because you no longer need to worry about spaces, you can simply do:
public string[] info13 = info12.Split(',');
Which will return a string array of the split items.
This question already has answers here:
How to get the last five characters of a string using Substring() in C#?
(12 answers)
Closed 8 years ago.
I have a string variable like test10015, i want to get just the 4 digits 1001,
what is the best way to do it?
i"m working in asp.net c#
With Linq:
var expected = str.Skip(4).Take(4);
Without Linq:
var expected = str.Substring(4,4);
Select the first four digits in your string:
string str = "test10015";
string strNum = new string(str.Where(c => char.IsDigit(c)).Take(4).ToArray());
You can use String.Substring Method (Int32, Int32). You can subtract 5 from from the length to start from your required index. Make sure the format of string remains the same.
string res = str.Substring(str.Length-5, 4);
string input = "test10015";
string result = input.Substring(4, 4);