Biswajitghosh25

Dear All,

I have taken a string in my code. I want to take the last two characters from this string. Can any body tell me how.

e.g. the string is "Biswajit". I want to display the last two characters that is "it" from the right. Can any one tell me how it will be in c#. by sending a small code snippets.

Regadrs,

Biswajit



Re: Windows Forms Data Controls and Databinding Take two string from a string

Derek Smyth

Hi mate,

This snippet will take the last two characters of a string

Code Snippet

myString.Substring(myString.Length - 2, 2)






Re: Windows Forms Data Controls and Databinding Take two string from a string

Biswajitghosh25

Dear derek Smyth,

Thanks. Its nicely working for me. Can you tell me How can i find a particular string from. E.g My string is "Bis-wajit Ghosh" and I would like to search the string "-" from Biswajit. How

Actually I would like to take the two string "is" and the "wajit" from the string. Can you help me by sending a small code snippets.

Regards,

Biswajit





Re: Windows Forms Data Controls and Databinding Take two string from a string

Derek Smyth

Hello again,

The string class has a few methods that let you parse a string. There is also another piece of technology called regular expressions that let you do all sorts of clever things with strings. This is just for information, sometimes parsing strings with the methods of the string class can get messy, if your in that situation then regular expressions are the better option.

I think you meant remove the '-' from your string, which you can do with the replace method like this.

Code Snippet

myString.Replace("-", "") 'vb

myString.Replace('-', '') //c#

A string is basically a series of characters, an array of characters, each character of the string has an index so your string

Bis-wajit Ghosh has character indexes like this

012345678...

Index 0 = char B, index 1 = i, index 2 = s

So the substring method, as well as other string methods, use the index. So if you wanted wajit (before the - is removed) you would call

Code Snippet

'myString.Substring(index, length)

myString.Substring(4, 5)'gives you wajit (start at 4th character and take 5 characters)