string 간격 조절(string interval)

조회 수: 7 (최근 30일)
창민 이
창민 이 2020년 8월 19일
답변: Dinesh Yadav 2020년 8월 24일
제가 원하는 구문 (예를들어 asdffdsaasdf) 을 일정한 간격으로 조절할 수 있나요? (예를들어 as df fd sa as df)
can i modify my string interval as manual? (as 'asdffdsaasdf' to 'as df fd sa as df')
직접 간격을 수정하고자 하는 부분은 plaintext구문이고, 35964개의 숫자가 띄어쓰기 없이 구성되어 있습니다. 간격은 2글자당 한칸씩 띄우고 싶습니다.
the string i wanna change is the plaintext, consist of 35964 numbers without spacing. i wannna space one in two texts.

채택된 답변

Dinesh Yadav
Dinesh Yadav 2020년 8월 24일
There are 3 methods stated as follows:
Let the string be
S = 'asdffdsaasdf'
The first method is to decompose the string into individual cells, then join them via strjoin with spaces:
str = mat2cell(S, 1, 2*ones(1,numel(S)/2));
outStr = strjoin(str, ' ');
In second method you can use regular expressions to count up exactly 2 characters per token, then insert a space at the end of each token, and trim out any white space at the end if there happen to be spaces at the end:
outStr = strtrim(regexprep(S, '.{2}', '$0 '));
In third method you can reshape your character matrix so that each pair of characters is a column, you would insert another row full of spaces, then reshape back. Also trim out any unnecessary whitespace:
str = reshape(S, 2, []);
str(3,:) = 32*ones(1,size(str,2));
outStr = strtrim(str(:).');

추가 답변 (0개)


Help CenterFile Exchange에서 문자형과 string형에 대해 자세히 알아보기


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!