string 간격 조절(string interval)

2 ビュー (過去 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 Center および File Exchange문자형과 string형 についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!