Hello,

I am working on aprogram to convert string to hex ..... its working 100%

it takes file name as input read it line by line , convert each line to hex character by character, and write it to file.

the problem is when the string look like this :

"sdfg|wer|asdfe" ............I need ever chars between ||(for example "|wer|") to remain the same(to be written to the output without being converted) in the output and the other part of the string to be converted .


any ideas how to do it ?

Thanks

Will there only ever be one part per line or multiple?

eg. abcd|efg|hij or abc|def|ghi|jkl|mno If it's the first one, simply way is to do String.Split("|") and only convert [0] and [2] of the array.

If it's the latter, you will need to do some calculations using String.FirstIndexOf("|") to find each position and then grab out the text you need.

Thank you for your reply, its the first one" abcd|efg|hij"
but the problem is when it come like this "|asd|sdfsdf" , in this case I need to convert [1] only !

how I can check if "|" is the first char or not? before I make the split?

Regards

String.FirstIndexOf("|") ..... is this function return the index of "|" as its like a char array?

To answer your second question, yes. It searches the string for the first instances of "|" and returns the position in the string that it finds it. Please note that the index returned is 1 based, not 0 based like arrays are. So if it's the first character in the sequence it will return 1, not 0.

When looking for your next "|" use the FindIndexOf function, but rather than starting at zero again, start at the position you last found your character at. It will then return the next index in the sequence. You can then use substring to pull out the characters in the middle.

What is left for you to do is then the logic of how to put it back together again, which you should already know how to do =)

Thank you for your help

string pattern = "|as%#@df|qwe  iop|awe|rty|uio|";

string[] words = pattern.Split('|');
           
 Console.WriteLine(words.length);//this line return 7(the length of the first substring "as%#@df") while it suppose to return 5(as%#@df ,qwe  iop, awe ,rty, uio) ..?

,\

the answer is that split just add tow extra possition one at the begining and one at the end of the word[] , you just have to subtract tow from

word.length

to get the right length.

To answer your second question, yes. It searches the string for the first instances of "|" and returns the position in the string that it finds it. Please note that the index returned is 1 based, not 0 based like arrays are. So if it's the first character in the sequence it will return 1, not 0.

This is false. It returns 0 if it is the first character, just like a normal array.

This is false. It returns 0 if it is the first character, just like a normal array.

I am talking about the length of the array after the split, if we expect 5 splits based on the separator, you will get 7..............what is your explanation of this?

My post refered to Ketsuekiame post, not yours.

As for your problem, you begin and end the string with the separator, thus you get two String.Empty entries in your array. If you don't want empty entries, tell split you don't want them:

string[] words = pattern.Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.