우리는 문자열이 0000029653
입니다. 어떤 값으로 숫자를 이동하는 법.
예를 들어, 4 시프트 한 후 결과는 다음과 같아야합니다. 0296530000
이 연산자 또는 함수가 있습니까?
감사합니다.문자열을 왼쪽으로 시프트
-3
A
답변
0
숫자를 int로 캐스팅하여 문자열로 되돌릴 수 있습니다. 당신은 숫자로 변환 할 수
String number = "0000029653";
String shiftedNumber = number.Substring(4);
+0
Downvote 정말요? :/ – Owen
4
다음을 수행하십시오
Result = yournumber * Math.Pow(10, shiftleftby);
는 0 왼쪽 문자열과 패드로 다시 변환
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;
}
+0
문자 배열로 복사본을 만들 필요가 없습니다. 문자열의 문자를 직접 인덱싱하여 액세스 할 수 있습니다 (예 :'numberStr [ j % i]')가 작동합니다. – Adam
2
당신이 원하지 않는 경우 하위 문자열 및 색인을 사용하는 경우 Linq과 함께 재생할 수도 있습니다.
string inString = "0000029653";
var result = String.Concat(inString.Skip(4).Concat(inString.Take(4)));
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)));
}
관련 문제
- 1. C 번호 : 왼쪽으로 시프트 할당 연산자의 동작
- 2. 문자열을 C에서 오른쪽에서 왼쪽으로 구문 분석
- 3. css float : 왼쪽으로 왼쪽으로 이동
- 4. PHP로 레터 시프트
- 5. 시프트 문자
- 6. 부호있는 시프트
- 7. 예기치 않은 왼쪽 시프트 행동
- 8. 배열을 시프트 할 포인터 C++
- 9. 시프트 문자열의 데이터 구조
- 10. sql은 CONCAT 문자열을 인수로 사용하여 두 테이블에 대해 왼쪽으로 결합합니다.
- 11. 매크로를 사용하여 해당 열의 셀 값이 임계 값 아래에 있으면 열을 삭제하고 Excel에서 왼쪽으로 시프트 하시겠습니까?
- 12. 오른쪽에서 왼쪽으로 표시 글꼴
- 13. 왼쪽으로 합치기
- 14. 비 즉시 시프트 값에 대한 sse 128 비트 시프트 연산 찾기
- 15. 이진 오른쪽 시프트, 주어진 경우에만 추가
- 16. ASP에서 비트 연산자 왼쪽 시프트 및 오른쪽 시프트
- 17. 컴파일러에서 "시프트/시프트"충돌이 발생하지 않는 이유는 무엇입니까?
- 18. 롱 타입, 왼쪽 시프트 및 오른쪽 시프트 작업
- 19. 아무 것도 없을 때 비트 시프트 ... 비트 시프트 연산자
- 20. 시프트 타이밍 계산 문제
- 21. C 비트 단위 시프트
- 22. 인터페이스 빌더에서 UIImage보기 시프트
- 23. 코코아에서 시프트 키 수정
- 24. 배열의 시프트 요소
- 25. 자바 시프트 연산자
- 26. 표현식 호출과의 시프트/줄이기
- 27. 제어 콘텐츠 시프트 행동
- 28. dismissModalViewControllerAnimated 시프트 다운보기
- 29. bash에서 파일의 시프트 바이트
- 30. Java에서 시프트 암호 만들기
예를 6으로 이동하면 결과가 어떻게됩니까? –
@HenkHolterman : 그 결과는 다음과 같아야합니다 : 6530000029 – user1260827
FYI : 그것은 회전이 아니라 이동이라고합니다. –