MATLAB Answers

string 간격 조절(string interval)

2 views (last 30 days)
창민 이
창민 이 on 19 Aug 2020
Answered: Dinesh Yadav on 24 Aug 2020
제가 원하는 구문 (예를들어 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.


Sign in to comment.

Accepted Answer

Dinesh Yadav
Dinesh Yadav on 24 Aug 2020
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(:).');


Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!