Home > OS >  Using substring and indexOf together
Using substring and indexOf together

Time:11-12

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.

CodePudding user response:

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

CodePudding user response:

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