I has a string like TPHSFL100 and TPHFL50. For the first string, i want get FL100, for second string i want get HFL50. How I can do this? I do not know which text user will key in, any dynamic solution for this or what algorithm I should use?
string text = "ABCDEF";
string sub = text.Substring(text.Length-5,text.Length);
Console.WriteLine("Substring: {0}", sub);
Related
I have searched up how to do this. All I got was removing text based on a index point. I would like to have it setup where it uses a string to remove text, instead of choosing the point.
Something along the lines of
string text1 = "Hello World!";
string text2 = "Hello";
string text3;
void RemoveText()
{
text3 = text1.Remove(text2);
Console.WriteLine(text3);
}
Output:
Console: World!
Is there a method that can remove text by using a string? I was looking at the Microsoft Docs, but the problem is that the string wouldn't have a denfinite spot that need to be removed.
text3 = text1.Replace(text2,string.empty);
If you wanted to get part of a user inputed responce, you would doing something along the lines of
string textToRemove = "set background ";
Console.WriteLine("Type 'set background ' then your color.");
string userInput = Console.ReadLine();
string answer = userInput.Replace(textToRemove, string.empty);
SetBackgroundColor(answer);
First what you want to do is set the text you don't care about/need
string textToRemove = "set background ";
Next you are going to tell the user what to input and get the input
Console.WriteLine("Type 'set background ' then your color.");
string userInput = Console.ReadLine();
We would then have to make a new string without the unnessary text
string answer = userInput.Replace(textToRemove, string.empty);
In this case we are using the input to set the background color. So we would write a function for changing the background color with a string argument for the color the user gave.
SetBackgroundColor(answer);
You could also use an If statment or an for/foreach statment to see if the user's text without the unneccasry text is in an array or list containing strings that are allowed to be used.
This question already has answers here:
C# DateTime to "YYYYMMDDHHMMSS" format
(18 answers)
Closed 5 years ago.
I developing a xamarin form app and I am assigning the current date time as a Filename for image. Currently the image is saved as "7202017 53150 PM.jpg". I want it to be saved like this "720201753150PM.jpg". How can I remove the space between the date and time?
I tried like below but it did not work.
string _imagename = DateTime.Now.ToString();
_imagename.Replace(" ", string.Empty);
Actually the String.Replace() Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string(in short it won't change the actual string) So you need to assign the result to another variable. And perform the replace operation there.
But Why you go for another replace? whynot use .ToString() like the following?
string format = "Mddyyyyhhmmsstt";
string _imagename = String.Format("{0}.jpg",DateTime.Now.ToString(format))
You need to assign the new value as string is immutable:
string _imagename = DateTime.Now.ToString();
_imagename = _imagename.Replace(" ", string.Empty);
This is fastest way I know:
Regex.Replace(_imagename, #"\s+", "")
Looking at your string ill also suggest replace spaces with a empty string. And you could do it by applying built in Replace method:
string _imagename = DateTime.Now.ToString();
_imagename = _imagename.Replace(" ", string.Empty);
If you want to order by file name I'd suggest to use a notation like yyyyMMddHHmmss. That way, with increasing date/time the sort order will also increase.
Other than that, strings are immutable in c#. Thus calling Replace does not change the original string. You need to assign the result to your variable as #Romano Zumbé pointed out.
You can just use a format string like the following (including the sorting suggestion):
string imagename = $"{DateTime.Now:yyyyMMddHHmmss}.jpg";
otherwise it would be:
string imagename = $"{DateTime.Now:Mddyyyyhmmsstt}.jpg";
So I have this file with a number that I want to use.
This line is as follows:
TimeAcquired=1433293042
I only want to use the number part, but not the part that explains what it is.
So the output is:
1433293042
I just need the numbers.
Is there any way to do this?
Follow these steps:
read the complete line
split the line at the = character using string.Split()
extract second field of the string array
convert string to integer using int.Parse() or int.TryParse()
There is a very simple way to do this and that is to call Split() on the string and take the last part. Like so if you want to keep it as a string:
var myValue = theLineString.Split('=').Last();
If you need this as an integer:
int myValue = 0;
var numberPart = theLineString.Split('=').Last();
int.TryParse(numberPart, out myValue);
string setting=sr.ReadLine();
int start = setting.IndexOf('=');
setting = setting.Substring(start + 1, setting.Length - start);
A good approach to Extract Numbers Only anywhere they are found would be to:
var MyNumbers = "TimeAcquired=1433293042".Where(x=> char.IsDigit(x)).ToArray();
var NumberString = new String(MyNumbers);
This is good when the FORMAT of the string is not known. For instance you do not know how numbers have been separated from the letters.
you can do it using split() function as given below
string theLineString="your string";
string[] collection=theLineString.Split('=');
so your string gets divided in two parts,
i.e.
1) the part before "="
2) the part after "=".
so thus you can access the part by their index.
if you want to access numeric one then simply do this
string answer=collection[1];
try
string t = "TimeAcquired=1433293042";
t= t.replace("TimeAcquired=",String.empty);
After just parse.
int mrt= int.parse(t);
I have a string:
string fileName = VAH007157100_REINSTMT_20d5fe49.tiff
I want to split this at the end of REINSTMT.
string splittedFileName = fileName.split("REINSTMT")[0];
The above does not work.
How would I go about splitting it to grab everything from the left side of the word "REINSTMT"?
Try this
string splittedFileName = fileName.Split(new string[]{"REINSTMT"},
StringSplitOptions.None)[0];
In order to split based on a string rather than a char, you need to provide a second argument. See the documentation here.
What you probably want is
string splittedFileName = fileName.split(new string[] {"REINSTMT"}, StringSplitOptions.None)[0];
Another way would be with substring:
string fileName = "VAH007157100_REINSTMT_20d5fe49.tiff";
string splittedFileName = fileName.Substring(0, fileName.IndexOf("REINSTMT"));
I have two strings:
string strone="what is your name?"
string strtwo="what is your name? what is your school name?"
Any of the strings could be greater here. What I need is to extract string from strtwo which is not in strone.
What I have tried is this:
IEnumerable<string> str=strtwo.Except(strone); //(returns only first character ie w)
I tried converting both strone and strtwo to string arrays but looping through each string one by one won't provide solution as strone may contain other characters in between.
What i require is to extract the entire string sequentially in strtwo that is not in strone.
you can try to extract the text from the second string in this fashion
string diff = strtwo.Replace(strone,"");
This should output you " what is your school name?" which is what you are looking out for else please do update the question with other example/cases.
The simplest way is using Replace:
string strone="what is your name?";
string strtwo="what is your name? what is your school name?";
string finalStr = strtwo.Replace(strone, "");
If your looking for something that compares more than just the First part of the strings and shows the diff on the end then have a look at the Diff Implementation, but basically your looking for This Algorithm
However if you are just looking for the difference on the end of a string look at #dasblinkenlight solution
string strone="what is your name?"
string strtwo="what is your name? what is your school name?"
string extractedString = strtwo.Replace(strone, "");
why not looking at the result of
strtwo.Split(new String[]{strone}, StringSplitOptions.RemoveEmptyEntries)
?