Add bits before last alphabet of every word

1 回表示 (過去 30 日間)
Balkar Singh
Balkar Singh 2020 年 4 月 11 日
コメント済み: Balkar Singh 2020 年 4 月 17 日
How to add watermarks bit before the last alphabet of every word of a string.
  6 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 4 月 12 日
Input: '202D'
Expected Output:'202DI'
right?
Balkar Singh
Balkar Singh 2020 年 4 月 14 日
Yes sir. Thanks

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2020 年 4 月 12 日
Try this:
str = 'MATLAB is a super awesome program';
words = strsplit(str)
for k = 1 : length(words)
thisString = words{k};
if length(thisString) >= 2
words{k} = sprintf('%s%c%c', thisString(1:end-1), char(hex2dec('2020D')), thisString(end));
end
end
% Reconstruct into a single string
words
str2 = [];
for k = 1 : length(words)
thisString = words{k};
str2 = sprintf('%s %s', str2, thisString);
end
str2
You'll see in the command window:
words =
1×6 cell array
{'MATLAB'} {'is'} {'a'} {'super'} {'awesome'} {'program'}
words =
1×6 cell array
{'MATLAB'} {'is'} {'a'} {'super'} {'awesome'} {'program'}
str2 =
' MATLAB is a super awesome program'
except that the FFFF in the boxes don't show up in MATLAB - they're just little empty boxes.
  2 件のコメント
Image Analyst
Image Analyst 2020 年 4 月 12 日
Sorry, the second words is not showing up in the web browser the same way it does in the MATLAB command window. In the MATLAB command window, you can see that there is a little box (representing the special non-printable character) just before the last letter of every word, and in the browse (before I submitted it) the little boxes has 4 F's in them.
Balkar Singh
Balkar Singh 2020 年 4 月 14 日
Thanks

サインインしてコメントする。

その他の回答 (2 件)

Ameer Hamza
Ameer Hamza 2020 年 4 月 12 日
編集済み: Ameer Hamza 2020 年 4 月 12 日
This example show how to add the Unicode U+202D before the last character of every word. It also ignores full stops (.) and comma (,) while making a replacement. Since this Unicode character is invisible and has no effect on visible effect on the left to right characters, you can see the replacement is being made by comparing the length of the strings.
str = 'A quick brown fox, jumps over the lazy dog.';
l1 = numel(str);
new_str = regexprep(str, '([A-Za-z][\s\.,])', [char(hex2dec('202D')) '$1']);
l2 = numel(new_str);
>> l1
l1 =
43
>> l2
l2 =
52
You can also see that the replacement are being made by adding a dummy character (say a) after U+202D.
str = 'A quick brown fox, jumps over the lazy dog.';
new_str = regexprep(str, '([A-Za-z][\s\.,])', [char(hex2dec('202D')) 'a$1']);
new_str =
'aA quicak browan foax, jumpas ovear thae lazay doag.'
  3 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 4 月 14 日
Balkar, Stephen's answer covers that case. Please check it.
Balkar Singh
Balkar Singh 2020 年 4 月 14 日
Thanks I got

サインインしてコメントする。


Stephen23
Stephen23 2020 年 4 月 12 日
Simpler:
>> str = 'A quick brown fox, jumps over the lazy dog';
>> out = regexprep(str,'(\w)\>','\x202D$1')
out =
A quick brown fox, jumps over the lazy dog
  12 件のコメント
Image Analyst
Image Analyst 2020 年 4 月 17 日
Is this your homework you're asking us to do? It sounds quirky enough, just like homework.
Balkar Singh
Balkar Singh 2020 年 4 月 17 日
No sir this is not my homework. I am at the learning stage.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by