I have a textbox that will always have a delimiter in between two words such as Houston|Texas
How do I get the length of the text before, and the length of the text after the '|' delimiter into two separate integers?
Try this:
string strTest = "Houston|Texas";
string[] strArr = strTest.Split('|');
int intFirst = strArr[0].Length; //Will result to 7
int intSecond = strArr[1].Length; //Will result to 5
This might do the trick for you
string ajks = "Houston|Texas";
List<int> LengthList = ajks.Split('|').Select(x => x.Length).ToList();
Well, you could use one of this function I like to compare with:
string last = str.Substring(str.LastIndexOf('|') + 1);
string first = str.Substring(str.LastIndexOf('|') - 1);
//added
int last = (str.Substring(str.LastIndexOf('|') + 1)).Length;
int first = (str.Substring(str.LastIndexOf('|') - 1)).Length;
Related
I am solving a problem in Volleyball. In volleyball sport we have five sets per match.
string[] arrMatchSummary = fromDict.matchsummary.SafeSplit(' ');
string firstSet = arrMatchSummary[0];
string firstSetResult = firstSet.Replace('-', ':');
string secondSet = arrMatchSummary[1];
string secondSetResult = secondSet.Replace('-', ':');
string thirdSet = arrMatchSummary[2];
string thirdSetResult = thirdSet.Replace('-', ':');
string fourthSet = arrMatchSummary[3];
string fourthSetResult = fourthSet.Replace('-', ':');
In arrMatchSummary i have 5 items in array like "25:18" etc.
When match has not started, this line is giving me index out of range exception:
string secondSet = arrMatchSummary[1];
because, when it is not started, there are no second set.
My question is:
Everything works in code, how to SKIP this index out of range and continue so that the program could work?
Thanks.
Check your variable with If block and if its null or empty simple don't do anything.
Why not use LINQ?
fromDict.matchsummary.SafeSplit(' ')
.Select(r => r.Replace('-',':'))
.ToArray();
Try this way:
string[] arrMatchSummary = fromDict.matchsummary.SafeSplit(' ');
for(int i = 0; i < arrMatchSummary.Length; i++)
{
arrMatchSummary[i] = arrMatchSummary[i].Replace('-', ':');
}
Please check variable "mystr" value where a "-" sign between two part of numbers. I want to find "-" then remove all character after that then I want find same "-" and remove all Character from first to till that. I know it's simple but not getting exact solution on c# due to I am new.
public void test()
{
string mystr = "1.30-50.50";
//first output I want is- "1.30"
//second output I want is- "50.50"
}
Use string.Split method:
var mystr = "1.30-50.50";
var result = mystr.Split('-');
var a = result[0]; //"1.30"
var b = result[1]; //"50.50"
you can also String.IndexOf method
string mystr = "1.30-50.50";
int indexOfDash = mystr.IndexOf('-');
string firsResult = mystr.Substring(0, indexOfDash);
string secondResult = mystr.Substring(indexOfDash + 1, mystr.Length - indexOfDash - 1);
Is there a built-in function or more efficient way to add character to a string X number of times?
for example the following code will add '0' character 5 times to the string:
int count = 5;
char someChar = '0';
string myString = "SomeString";
for(int i=0;i<count;i++)
{
myString = someChar + myString;
}
Use PadLeft() or PadRight()
An example for PadRight():
int count = 5;
char someChar = '0';
string myString = "SomeString";
myString = myString.PadRight(count + myString.Length, someChar);
// output -> "SomeString00000"
Remember the first parameter of either method is the total string length required hence why I am adding count to the original string length.
Likewise if you want to append the character at the start of the string use PadLeft()
myString = myString.PadLeft(count + myString.Length, someChar);
// output -> "00000SomeString"
string.Concat(Enumerable.Repeat("0", 5));
will return
"00000"
Refered from :Is there a built-in function to repeat string or char in .net?
You can also do it as:
string line = "abc";
line = "abc" + new String('X', 5);
//line == abcXXXXX
Take a look here, You can use PadRight() / PadLeft();
int count = 5;
char someChar = '0';
string myString = "SomeString";
var stringLength = myString.Length;
var newPaddedStringRight = myString.PadRight(stringLength + count, '0');
//will give SomeString00000
var newPaddedStringLeft = myString.PadLeft(stringLength + count, '0');
//will give 00000SomeString
Remember, a string is Immutable, so you'll need to assign the result to a new string.
You could also use StringBuilder. As the string size increases the += incurs a cost on array copy.
I have a file name: kjrjh20111103-BATCH2242_20111113-091337.txt
I only need 091337, not the txt or the - how can I achieve that. It does not have to be 6 numbers it could be more or less but will always be after "-" and the last ones before ."doc" or ."txt"
You can either do this with a regex, or with simple string operations. For the latter:
int lastDash = text.LastIndexOf('-');
string afterDash = text.Substring(lastDash + 1);
int dot = afterDash.IndexOf('.');
string data = dot == -1 ? afterDash : afterDash.Substring(0, dot);
Personally I find this easier to understand and verify than a regular expression, but your mileage may vary.
String fileName = kjrjh20111103-BATCH2242_20111113-091337.txt;
String[] splitString = fileName.Split ( new char[] { '-', '.' } );
String Number = splitString[2];
Regex: .*-(?<num>[0-9]*). should do the job. num capture group contains your string.
The Regex would be:
string fileName = "kjrjh20111103-BATCH2242_20111113-091337.txt";
string fileMatch = Regex.Match(fileName, "(?<=-)\d+", RegexOptions.IgnoreCase).Value;
String fileName = "kjrjh20111103-BATCH2242_20111113-091337.txt";
var startIndex = fileName.LastIndexOf('-') + 1;
var length = fileName.LastIndexOf('.') - startIndex;
var output = fileName.Substring(startIndex, length);
this might be simple question I have 3 strings
A123949DADWE2ASDASDW
ASDRWE234DS2334234234
ZXC234ASD43D33SDF23SDF
I want to split those by the first 8 characters and then the 10th and 11th and then combine them into one string.
So I would get:
A123949DWE
ASDRWE23S2
ZXC234AS3D
Basically the 9th character and anything after the 12th character is removed.
You can use String.Substring:
s = s.Substring(0, 8) + s[10] + s[11]
Example code:
string[] a = {
"A123949DADWE2ASDASDW",
"ASDRWE234DS2334234234",
"ZXC234ASD43D33SDF23SDF"
};
a = a.Select(s => s.Substring(0, 8) + s[10] + s[11]).ToArray();
Result:
A123949DWE
ASDRWE23S2
ZXC234AS3D
So let's say you have them declared as string variables:
string s1 = "A123949DADWE2ASDASDW";
string s2 = "ASDRWE234DS2334234234";
string s3 = "ZXC234ASD43D33SDF23SDF";
You can use the substring to get what you want:
string s1substring = s1.Substring(0,8) + s1.Substring(9,2);
string s2substring = s1.Substring(0,8) + s1.Substring(9,2);
string s3substring = s1.Substring(0,8) + s1.Substring(9,2);
And that should give you what you need. Just remember, that the string position is zero-based so you'll have to subtract one from the starting position.
So you could do:
string final1 = GetMyString("A123949DADWE2ASDASDW");
string final2 = GetMyString("ASDRWE234DS2334234234");
string final3 = GetMyString("ZXC234ASD43D33SDF23SDF");
public function GetMyString(string Original)
{
string result = Original.Substring(12);
result = result.Remove(9, 1);
return result;
}