30 string Common Operations

Source: Internet
Author: User
Tags array to string

From: http://www.dotnetcurry.com/ShowArticle.aspx? Id = 189

The following two strings are used as an example:

 
StringStroriginal ="These functions will come handy";StringStrmodified =String. Empty;

1. iterate a string-You can use the 'For 'loop or 'foreach' loop to iterate through a string. The 'For 'loop gives you more flexibility over the iteration.

 
For(IntI = 0; I <stroriginal. length; I ++) {MessageBox. Show (stroriginal [I]. tostring ());}Foreach(CharCInStroriginal) {MessageBox. Show (C. tostring ());}

2. Split a string-You can split strings using string. split (). the method takes an array of chars, representing characters to be used as delimiters. in this example, we will be splitting the stroriginal string using 'space' as delimiter.

 
Char[] Delim = {''};String[] Strarr = stroriginal. Split (delim );Foreach(StringSInStrarr) {MessageBox. Show (s );}

3. Extract substrings from a string-The string. substring () retrieves a substring from a string starting from a specified character position. You can also specify the length.

// Only starting position specifiedStrmodified = stroriginal. substring (25); MessageBox. Show (strmodified );// Starting position and length of string to be extracted specifiedStrmodified = stroriginal. substring (20, 3); MessageBox. Show (strmodified );

4. Create a String Array-There are different ways to create a single dimenbetween and multi dimenbetween string arrays. Let us have e some of them:

 // Single dimen1_string Array  String [] Strarr = New String [3] { "String 1" , "String 2" ,"String 3" }; // Omit size of Array  String [] Strarr1 = New String [] { "String 1" , "String 2" , "String 3" }; // Omit New Keyword  String [] Strarr2 = { "String 1" , "String 2" , "String 3" };// Multi dimen1_string Array  String [,] Strarr3 = New String [2, 2] { "String 1" , "String 2" },{ "String 3" , "String 4" }}; // Omit size of Array  String [,] Strarr4 = New String [,] { "String 1" , "String 2" },{"String 3" , "String 4" }}; // Omit New Keyword  String [,] Strarr5 = {{ "String 1" , "String 2" },{ "String 3" , "String 4" }};

5. Reverse a string-One of the simplest ways to reverse a string is to use the strreverse () function. To use it in C #, you need to add a reference to the Microsoft. VisualBasic DLL.

StringStrmodified = Microsoft. VisualBasic. Strings. strreverse (stroriginal );
MessageBox. Show (strmodified );

6. Compare two strings-You can use the string. Compare () to compare two strings. The third parameter is a Boolean parameter that determines if the search is case sensitive (false) or not (true ).

If((String. Compare (stroriginal, strmodified,False) <0) {MessageBox. Show ("Stroriginal is less than stroriginal1");}Else if((String. Compare (stroriginal, strmodified,False)> 0) {MessageBox. Show ("Stroriginal is more than stroriginal1");}Else if((String. Compare (stroriginal, strmodified,False) = 0) {MessageBox. Show ("Both strings are equal");}

7. convert a string to byte [] (byte array)-The encoding. getbytes () encodes all the characters into a sequence of bytes. The method contains six overloads out of which we will be using the encoding. getbytes (string ).

 
Byte[] B = encoding. Unicode. getbytes (stroriginal );

8. Convert byte [] to string-The encoding. getstring () decodes a sequence of bytes into a string.

 
// Assuming you have a byte array byte [] BStrmodified = encoding. Unicode. getstring (B );

9. convert a string to Char [] (char array)-To convert a string to char array, use the string. tochararray () which copies the characters in the string to a Unicode character array.

Char[] Charr = stroriginal. tochararray ();

10. convert a char [] to string-A convenient way to convert a character array to string is to use the string constructor which accepts a character array

 
Strmodified =NewString (Charr );

11. test if string is null or zero length-A simple way to test if a string is null or empty is to use the string. isnullorempty (string) which returns a Boolean value.

 
BoolCheck =String. Isnullorempty (stroriginal );

12. Convert the case of a string-The string class contains methods to convert a string to lower and upper cases. however, it lacks a method to convert a string to proper case/Title case. hence we will use the 'textinfo' class to do the same.

 
System. Globalization.CultureinfoCultureinfo = system. threading.Thread. Currentthread. currentculture; system. Globalization.TextinfoTextinfo = cultureinfo. textinfo;// Lower caseMessageBox. Show (textinfo. tolower (stroriginal ));// Upper caseMessageBox. Show (textinfo. toupper (stroriginal ));// Proper caseMessageBox. Show (textinfo. totitlecase (stroriginal ));

13. Count the occurrences of words in a string-You can adopt multiple ways to find the occurrence of a word in a string. one of them is to use the string. indexof () which is one of the ways of finding the occurrence of the word. in VB. net, use string. instr ().

Another simple way is to use 'Count' property of the RegEx. Matches () collection. However this method is slow. We will need e both these methods in the sample.

 // Using indexof  Int Strt = 0; Int CNT =-1; Int Idx =-1; stroriginal = "She sells sea shells on the sea shore" ; String Srchstring ="Sea" ; While (Strt! =-1) {strt = stroriginal. indexof (srchstring, idx + 1); CNT + = 1; idx = strt;} MessageBox. Show (srchstring + "Occurs" + CNT + "Times" ); // Using regular expression System. Text. regularexpressions. RegEx REX = New System. Text. regularexpressions. RegEx (srchstring ); Int Count = Rex. Matches (stroriginal). Count; MessageBox. Show (srchstring + "Occurs" + Count + "Times" );

14. Insert characters inside a string-The string. insert () inserts text at a specified index location of a string. you can insert either a character or a string at a given index location. for eg: We will insert a string "very" at index 26 in string stroriginal.

 
Strmodified = stroriginal. insert (26,"Very"); MessageBox. Show (strmodified );

15. Replace characters in a string-The string. Replace () removes characters from a string and replaces them with a new character or string.

 
Strmodified = stroriginal. Replace ("Come handy","Be useful"); MessageBox. Show (strmodified );

/Files/chenqingwei/30 serial numbers for common operations. rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.