Omitting the first digit from a given number [duplicate] - c#

This question already has answers here:
How do I get the last four characters from a string in C#?
(27 answers)
Closed 3 years ago.
I am given a contact number by a customer.
The client that this number is given to requires the first number to be omitted from the result.
I have used Regex to do this, but I'm curious if there is a more optimal way to do this.
var mobileNumber = "07123123123";
var homeNumber = "01511231231";
var pattern = "(.{10})$";
var omittedMobile = Regex.Split(mobileNumber, pattern)[1];
var omittedHome = Regex.Split(homeNumber, pattern)[1];
var mobileNumber = "07123123123";
var homeNumber = "01511231231";
I receive: 07123123123 - I provide 7123123123

Why not treat it as a simple string and remove the first character?
mobileNumber.Substring(1);
//or
mobileNumber.Remove(0, 1);

Using string function Substring(int startIndex),
var mobileNumber = "07123123123";
Console.WriteLine(mobileNumber.Substring(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 to convert int variables to string variables [duplicate]

This question already has answers here:
Convert int to string?
(12 answers)
Closed 4 years ago.
I am currently splitting a string from a textbox that the user will fill with three numbers. Those numbers i want to be saved as seperate integers. Any help as to how to do this? Thanks for any help!
string[] count = txtPoemInput.Text.Split('/'); //Splitting values for keyword numbers
int Poem, Line, Word;
count[0] = Poem.ToString; // Example
count[1] = Line; // Example
count[2] = Word;
Here is what you need to do. Use Convert.ToInt32
Poem = Convert.ToInt32(count[0]);
Line = Convert.ToInt32(count[1]);
Word = Convert.ToInt32(count[2]);

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();

C# Capitalise First Letter of String - More effective method? [duplicate]

This question already has answers here:
Converting string to title case
(23 answers)
Closed 7 years ago.
I have some code that does what I want it to do but I want to know if there's a quicker way to do what I want.
The user will input their names and I want to make sure that their name is as close to this format as possible:
john > John
julie > Julie
My code:
// First Name
s_in_GetUserFirstName = s_in_GetUserFirstName.ToLower();
c_in_UserFirstNameFirstChar = s_in_GetUserFirstName[0];
s_in_UserFirstNameFirstChar = c_in_UserFirstNameFirstChar.ToString().ToUpper();
s_in_GetUserFirstName = s_in_GetUserFirstName.Remove(0, 1);
s_in_GetUserFirstName = s_in_UserFirstNameFirstChar + s_in_GetUserFirstName;
// Last Name
s_in_GetUserLastName = s_in_GetUserLastName.ToLower();
c_in_UserLastNameFirstChar = s_in_GetUserLastName[0];
s_in_UserLastNameFirstChar = c_in_UserLastNameFirstChar.ToString().ToUpper();
s_in_GetUserLastName = s_in_GetUserLastName.Remove(0, 1);
s_in_GetUserLastName = s_in_UserLastNameFirstChar + s_in_GetUserLastName;
What I do is split the string into two parts, the first char and the rest. I capitalise the first character and remove that character from the string. Then I combine strings into one.
You can do this in a single line like this
test.Substring(0,1).ToUpper()+test.Substring(1);
Or do this as Rob suggested in comment
Converting string to title case

Finding the substring [duplicate]

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);

Categories

Resources