C# Insert

来源:百度知道 编辑:UC知道 时间:2024/06/15 13:46:01
using System;
using System.Text;

namespace Zfcjf
{
public class program
{
public static void Main()
{
Console.WriteLine("请输入第一组数字");
string a = Console.ReadLine();
Console.WriteLine("请输入第二组数字");
string b = Console.ReadLine();
if (a.Length > b.Length)
{
b = b.PadLeft(a.Length, '0');
}
else
{
a = a.PadLeft(b.Length, '0');
}
StringBuilder s = new StringBuilder();
int c = 0;
for (int i = (a.Length - 1); i >= 0; i--)
{
int x = (a[i] - 0x30) + (b[i] - 0x30) + c;
s.Insert(0, x % 10);
c = x / 10;
}
if (c != 0)

把每次求得C的值,从后向前插入到StringBuilder的s中去。

insert是StringBuilder类的一个方法,MSDN中解释其作用是:
将指定对象的字符串表示形式插入到此实例中的指定字符位置。
public StringBuilder Insert (
int index,
int value
)
参数
index
此实例中开始插入的位置。

value
要插入的值。

返回值
完成插入操作后对此实例的引用。