Inserts a character into a string in ascending order

Source: Internet
Author: User
Tags insert return string tostring
 
 
  1. Namespace Tools.module
  2. {
  3. public class Tools
  4. {
  5. Public Tools ()
  6. {
  7. }
  8. public static string Getstrfromstr (String Src,int Index)
  9. {
  10. if (src. IndexOf (",") ==0) src = src. Remove (0,1);
  11. for (int i=0;i<index;i++)
  12. {
  13. src = src. Remove (0,1+SRC. IndexOf (","));
  14. }
  15. if (src. IndexOf (",") <0) return to SRC;
  16. else return SRC. Substring (0,SRC. IndexOf (","));
  17. }
  18. <summary>
  19. Calculates where a character should be located in an ascending sort string.
  20. </summary>
  21. <param name= "src" > string </param>
  22. <param name= "C" > Character </param>
  23. <returns> where the character should be located </returns>
  24. public static int Getthesortedindex (string Str,char c)
  25. {
  26. int ilength = str. Length;
  27. if (Ilength = = 0) return 0;
  28. if (ilength = = 1 && str[0] >= c) return 0;
  29. else if (ilength = = 1 && str[0] < C) return 1;
  30. int index = ILENGTH/2;
  31. if (str[index]>c)
  32. {
  33. return Getthesortedindex (str. Substring (0,index), c);
  34. }
  35. else if (str[index]<c)
  36. {
  37. return Index+1+getthesortedindex (str. Substring (index+1,ilength-index-1), c);
  38. }
  39. Else
  40. return index;
  41. }
  42. <summary>
  43. Inserts a character into a string in ascending order
  44. </summary>
  45. <param name= "C" > characters to insert </param>
  46. <param name= "str" > Purpose string </param>
  47. public static void Insertcharintosortedstring (char c,ref string str)
  48. {
  49. if (Str==null str. length==0)
  50. {
  51. Str=c.tostring ();
  52. Return
  53. }
  54. for (int i=0;i<str. length;i++)
  55. {
  56. if (str[i]==c) return;
  57. }
  58. int index = Getthesortedindex (STR,C);
  59. str = str. Insert (Index,c.tostring ());
  60. }
  61. }
  62. }


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.