I have a string like this. I need to take after first 8 character until first comma. I tried many things but i can't figure it out? Could someone help? String comes like this.
"asTyped:fTestManager.Jobs.TestReplications.TestDataReplicationsJob,Version=1.0.0.0,"
I need convert this string to
"TestManager.Jobs.TestReplications.TestDataReplicationsJob"
I've tried
string tmp= "asTyped:fTestManager.Jobs.TestReplications.TestDataReplicationsJob,Version=1.0.0.0,";
int tmpLength = tmp.Substring(9).Length;
string test = tmp.Substring(tmp.IndexOf(',') - tmpLength)
I don't know im trying somethings like this but i can't make it out. Thank you all.
Since we know the initial text is always the same 9 characters, we can simplify things somewhat:
string tmp= "asTyped:fTestManager.Jobs.TestReplications.TestDataReplicationsJob,Version=1.0.0.0,";
int startPos = 9;
int comma = tmp.IndexOf(',', startPos);
string test = tmp.Substring(startPos, comma-startPos);
string str = "asTyped:fTestManager.Jobs.TestReplications.TestDataReplicationsJob,Version=1.0.0.0,";
string result = str.Split(',')[0].Substring(9);
First we split our string with , character with the Split(char separator) method. Then we get from index 9 to the end using Substring(int startIndex).
Related
I need to get the index of a specific character in a string, but I need to know if it occurs twice in that string and if it is so, get the second occurrence index.
I tried a few things but couldn't find the solution. Does anyone know how can it be done in .Net? Vb.net if possible.
What I am trying is explained below:
I have string like : 01298461705691703
I need to aquire the index of 17 in this string but if the string has two 17's then I need to know the index of the second one.
You will need to use IndexOf two times, using its overload on the second time.
string myStr = "01298461705691703";
// Find the first occurence
int index1 = myStr.IndexOf("17");
// You might want to check if index1 isn't -1
// Find the second occurrence, starting from the previous one
int index2 = myStr.IndexOf("17", index1 + 1);
// We add +1 so that it doesn't give us the same index again
// Result will be 13
See: https://learn.microsoft.com/en-us/dotnet/api/system.string.indexof
The solution is String.LastIndexOf
Dim myStr as String = "01298461705691703"
Dim idx as Integer = myStr.LastIndexOf("17")
or in c#
string myStr = "01298461705691703";
int idx = myStr.LastIndexOf("17");
Start your string from first occurrence 17, something similar to
string str = "01298461705691703";
int i = str.IndexOf("17", s.IndexOf("17")+1);
//^^^^^^^^^^^^^^^^^ This will start your string from first occurrence of 17
Syntax of indexOf
string.IndexOf(Char, Int32)
where,
char is a unicode character to seek.
Int32 is starting index of string
If you are trying to find out last occurrence of 17 in string, then you can use string.LastIndexOf() method.
string str = "01298461705691703";
int lastIndex = str.LastIndexOf("17");
POC : .Net Fiddle
I learn c# and I have a question about strings. There is a string with full of '1's and '0's. But I don't know its length. I get the length of string with while loop. But if length is less than 8, I need to complete its length to 8 with '0's. I tried to declare a new string with 7 '0's. And did this: (where a is my string and zeroAdd is the '0's I want to add.
if(length<8)
{
for(i=length;i<8;i++)
a[i]=zeroAdd[8-length];
a[i]='\0';
}
but it didn't work. I can't use the shortcuts I saw on the internet and wanted to ask you. I would appreciate if you could explain it to me like this. Thanks in advance. Have a good one.
Why not use the inbuilt padding?
string myString = "1101";
var result = myString.PadLeft(8,'0');
Gives you:
00001101
To do it with a loop (and right-padding), you can do this:
string myString = "1101";
while (myString.Length < 8)
myString += "0";
Ok guys so I've got this issue that is driving me nuts, lets say that I've got a string like this "aaa,bbb,ccc,ddd,eee,fff,ggg" (with out the double quotes) and all that I want to get is a sub-string from it, something like "ddd,eee,fff,ggg".
I also have to say that there's a lot of information and not all the strings look the same so i kind off need something generic.
thank you!
One way using split with a limit;
string str = "aaa,bbb,ccc,ddd,eee,fff,ggg";
int skip = 3;
string result = str.Split(new[] { ',' }, skip + 1)[skip];
// = "ddd,eee,fff,ggg"
I would use stringToSplit.Split(',')
Update:
var startComma = 3;
var value = string.Join(",", stringToSplit.Split(',').Where((token, index) => index > startComma));
Not really sure if all things between the commas are 3 length. If they are I would use choice 2. If they are all different, choice 1. A third choice would be choice 2 but implement .IndexOf(",") several times.
Two choices:
string yourString="aaa,bbb,ccc,ddd,eee,fff,ggg";
string[] partsOfString=yourString.Split(','); //Gives you an array were partsOfString[0] is "aaa" and partsOfString[1] is "bbb"
string trimmed=partsOfString[3]+","+partsOfString[4]+","+partsOfString[5]+","+partsOfSting[6];
OR
//Prints "ddd,eee,fff,ggg"
string trimmed=yourString.Substring(12,14) //Gets the 12th character of your string and goes 14 more characters.
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 coded string that I'd like to retrieve a value from. I realize that I can do some string manipulation (IndexOf, LastIndexOf, etc.) to pull out 12_35_55_219 from the below string but I was wondering if there was a cleaner way of doing so.
"AddedProject[12_35_55_219]0"
If you can be sure of the format of the string, then several possibilities exist:
My favorite is to create a very simple tokenizer:
string[] arrParts = yourString.Split( "[]".ToCharArray() );
Since there is a regular format to the string, arrParts will have three entries, and the part you're interested in would be arrParts[1].
If the string format varies, then you will have to use other techniques.
So in summary, if you have a pattern that you can apply to your string, the easiest is to use regular expressions, as per Guffa example.
On the other hand you have different tokens all the time to define the start and end of your string, then you should use the IndexOf and LastIndexOf combination and pass the tokens as a parameter, making the example from Fredrik a bit more generic:
string GetMiddleString(string input, string firsttoken, string lasttoken)
{
int pos1 = input.IndexOf(firsttoken) + 1;
int pos2 = input.IndexOf(lasttoken);
string result = input.Substring(pos1 , pos2 - pos1);
return result
}
And this is assuming that your tokens only happens one time in the string.
That depends on how much the string can vary. You can for example use a regular expression:
string input = "AddedProject[12_35_55_219]0";
string part = Regex.Match(input, #"\[[\d_]+\]").Captures[0].Value;
There are two methods which you may find useful, there is IndexOf and LastIndexOf with the square brackets as your parameters. With a little bit of research, you should be able to pull out the project number.
Here is a improvement from Wagner Silveira's GetMiddleString
string GetMiddleString(string input, string firsttoken, string lasttoken)
{
int pos1 = input.ToLower().IndexOf(firsttoken.ToLower()) + firsttoken.Length;
int pos2 = input.ToLower().IndexOf(lasttoken.ToLower());
return input.Substring(pos1 , pos2 - pos1);
}
And here how you use it
string data = "AddedProject[12_35_55_219]0";
string[] split = data.Split("[]".ToCharArray());
rtbHelp.Text += GetMiddleString(data, split[0], split[2]).Trim("[]".ToCharArray());//print it to my C# winForm RichTextBox Help