Finding the substring [duplicate] - c#

This question already has answers here:
How to get the last five characters of a string using Substring() in C#?
(12 answers)
Closed 9 years ago.
I have a string that could look like this: smithj_Website1 or it could look like this rodgersk_Website5 etc, etc. I want to be able to store in a string what is after the "_". So IE (Website1, Website5,..)
Thanks

Should be a simple as using substring
string mystr = "test_Website1"
string theend = mystr.SubString(mystr.IndexOf("_") + 1)
// theend = "Website1"
mystr.IndexOf("_") will get the position of the _ and adding one to it will get the index of the first character after it. Then don't pass in a second parameter and it will automatically take the substring starting at the character after the _ and stopping and the end of the string.

int startingIndex = inputstring.IndexOf("_") + 1;
string webSite = inputstring.Substring(startingIndex);
or, in one line:
string webSite = inputstring.Substring(inputstring.IndexOf("_") + 1);

Related

Is it possible to convert Hello.World to ["Hello"]["World"]? [duplicate]

This question already has answers here:
Put result of String.Split() into ArrayList or Stack
(5 answers)
Closed 2 years ago.
I was wondering if there is a way to convert example:
'Hello.World' into '["Hello"]["World"]' or 'This.is.a.string' into ["This"]["is"]["a"]["string"]
I'm kinda new to C# and I was wondering if that's even possible using string formatting or something like that.
You can use the Split method like this string[] strings = String.Split("."); This will split your string per each period.
If you meant to include the single quotes (') as part of the string then:
String test = "'Hello.World'";
// Strip the first and last '
test = test.Substring(1, test.Length - 2);
// Split on Period
String[] split = test.Split('.');
// Encapsulate each word with [" "]
// and add back in the single quotes
var result = $"'{String.Join("", split.Select(word => $"[\"{word}\"]"))}'";
Prints:
'["Hello"]["World"]'
If they just meant to surround your input then just:
String test = "Hello.World";
// Split on Period
String[] split = test.Split('.');
// Encapsulate each word with [" "]
var result = $"{String.Join("", split.Select(word => $"[\"{word}\"]"))}";
Prints: ["Hello"]["World"]

How can i filter a string by first few char [duplicate]

This question already has answers here:
To get specific part of a string in c# [closed]
(5 answers)
Closed 4 years ago.
I have a string variable called price which contains a range of price pattern as you can see in code. I want to filter it by its first part only.
string price = "9.99 - 12.99";
i already tried like bellow but this gives me wrong output something like ".99" however this is not my target output i am looking for.
string result = price.Substring(1, price.IndexOf("-") - 1);
The output i want like this- "9.99". Now can i filter this part from that string? Thanks in advance
Strings in C# are zero-based, so when you set the starting character as 1, you're in fact starting from the second character. Just use 0 and you should be OK:
string result = price.Substring(0, price.IndexOf("-") - 1);
Note, by the way, that you could use the full " - " delimiter instead of playing around with index arithmetic:
string result = price.Substring(0, price.IndexOf(" - "));
You're close but note that strings are 0 indexed:
string result = price.Substring(0, price.IndexOf("-") - 1);

Second to the LastIndexOf C# [duplicate]

This question already has answers here:
Get Second to last character position from string
(10 answers)
Closed 5 years ago.
* I cannot delete this duplicate question because someone has answered it *
I have file names formatted CustomerInfoDaily.12042014.080043 and CustomerInfoDaily.A.12042014.080043 I'm trying to get the base name (CustomerInfoDaily) and the base suffix (.12042014.080043) using substrings. There is no limit to the number of periods however the suffix is always .\d{8}.\d{8}
string fn = "CustomerInfoDaily.A.12042014.080043";
string baseFileName = fn.Substring(0, fn.LastIndexOf(".",fn.Length-1,fn.Length));
string baseSuffix = fn.Substring(fn.LastIndexOf(".", 0, 2));
The problem is that you can say you want the first or last dot but there is no saying that you want the second to the last instance of the dot.
Any help or advice would be greatly appreciated.
Consider using string.Split:
string fn = "CustomerInfoDaily.A.12042014.080043";
var split = fn.Split('.');
var last = split.LastOrDefault();
var secondLast = split.Skip(split.Length - 2).FirstOrDefault();

Capitalizing the first letter of a string only [duplicate]

This question already has answers here:
Make first letter of a string upper case (with maximum performance)
(42 answers)
Closed 7 years ago.
I have already taken a look at such posts like:
Format to first letter uppercase
How to capitalise the first letter of every word in a string
But none of these seem to actually work. I would have thought to start with that there would just be a:
.Capitalize();
Like there is:
.Lower(); & .Upper();
Are there any documentation or references regarding converting to a string like the following?
string before = "INVOICE";
To then becoming:
string after = "Invoice";
I receive no errors using the way the posts solutions I read give me, however, the before still remains capitalized.
What about using ToUpper on the first char and ToLower on the remaining string?
string after = char.ToUpper(before.First()) + before.Substring(1).ToLower();
You can create a method that does something like this:
string UppercaseFirst(string str)
{
if (string.IsNullOrEmpty(str))
return string.Empty;
return char.ToUpper(str[0]) + str.Substring(1).ToLower();
}
And use it like this:
string str = "thISstringLOokSHorribLE";
string upstr = UppercaseFirst(str);
to get this:
Thisstringlookshorrible

Remove string from a string [duplicate]

This question already has answers here:
How to remove a string from a string
(4 answers)
Closed 8 years ago.
I've this kind of string.
FullName:ae876ggfg777878848adgf877
And I want to remove "FullName:", so the output as follows:
ae876ggfg777878848adgf877
How can I do that?
I tried this:
var index = myText.IndexOf(":");
var result = myText.Remove(index);
But the output is like this:
FullName
Which I do not expect.
IndexOf returns the index of whatever string/character you give it, so in your case, the index of :.
Remove, according to the documentation:
Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.
So what's happening here is you're removing everything after and including the :
You should be using String.Replace:
string removed = myText.Replace("FullName:", "");
Use String.Substring() and get the start index by using String.IndexOf('character') + 1.
string s = "FullName:ae876ggfg777878848adgf877";
Console.WriteLine(s.Substring(s.IndexOf(':')+1));

Categories

Resources