文字列があります: 0000029653
. 数値をある値だけシフトする方法。
たとえば、4 ずつシフトすると、結果は次のようになります。0296530000
このための演算子または関数はありますか?
ありがとう
3931 次
5 に答える
4
それを数値に変換してから、次のようにします。
Result = yournumber * Math.Pow(10, shiftleftby);
次に、それを文字列に変換し、左に 0 を埋め込みます
于 2012-09-18T09:32:01.530 に答える
2
部分文字列とインデックスを使用したくない場合は、Linq で遊ぶこともできます。
string inString = "0000029653";
var result = String.Concat(inString.Skip(4).Concat(inString.Take(4)));
于 2012-09-18T11:59:03.810 に答える
1
public string Shift(string numberStr, int shiftVal)
{
string result = string.Empty;
int i = numberStr.Length;
char[] ch = numberStr.ToCharArray();
for (int j = shiftVal; result.Length < i; j++)
result += ch[j % i];
return result;
}
于 2012-09-18T09:38:21.607 に答える
0
数値を int として文字列にキャストして戻すことができます。
String number = "0000029653";
String shiftedNumber = number.Substring(4);
于 2012-09-18T09:31:36.137 に答える
0
以下のメソッドは、文字列をシフト/回転する回数を示す数値 n を取ります。数値が文字列の長さより大きい場合、文字列の長さで MOD を取得しました。
public static void Rotate(ref string str, int n)
{
if (n < 1)
throw new Exception("Negative number for rotation"); ;
if (str.Length < 1) throw new Exception("0 length string");
if (n > str.Length) // If number is greater than the length of the string then take MOD of the number
{
n = n % str.Length;
}
StringBuilder s1=new StringBuilder(str.Substring(n,(str.Length - n)));
s1.Append(str.Substring(0,n));
str=s1.ToString();
}
///You can make a use of Skip and Take functions of the String operations
public static void Rotate1(ref string str, int n)
{
if (n < 1)
throw new Exception("Negative number for rotation"); ;
if (str.Length < 1) throw new Exception("0 length string");
if (n > str.Length)
{
n = n % str.Length;
}
str = String.Concat(str.Skip(n).Concat(str.Take(n)));
}
于 2013-09-15T18:32:46.810 に答える