I am supposed to separate a string 'abcdef@eng.abc.edu' into three separate string 'abcdef', 'eng.abc', 'edu' using strsplit function. However, I don't know how to split the string at the last . delimiter instead of separate them into four strings using C = strsplit('abcdef@eng.abc.edu',{'@','.'}). Please help, thanks!

댓글 수: 1

dpb
dpb 2016년 11월 26일
Well, you've got to have some other rule that tells you to not consider one delimiter a delimiter at all...what is that rule? You'd have to start by separating out the domain from the address then parse off the last TLD (top-level domain) by searching for the last dot.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2016년 11월 26일

1 개 추천

lastdot_pos = find(YourString == '.', 1, 'last');

댓글 수: 3

Haotian Shen
Haotian Shen 2016년 11월 27일
Thank though, but this doesn't work because after finding the last delimiter, it is still the same '.' , which appeared twice in the string.
YourString = 'abcdef@eng.abc.edu';
lastdot_pos = find(YourString == '.', 1, 'last');
part12 = YourString(1 : lastdot_pos - 1);
part12cell = strsplit(part12, '@');
part1 = part12cell{1};
part2 = part12cell{2};
part3 = YourString(lastdot_pos+1 : end );
Alternative:
YourString = 'abcdef@eng.abc.edu';
part123cell = regexp(YourString, '@|\.(?=\w+$)', 'split');
part1 = part123cell{1};
part2 = part123cell{2};
part3 = part123cell{3};
Haotian Shen
Haotian Shen 2016년 11월 27일
Thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

태그

질문:

2016년 11월 26일

댓글:

2016년 11월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by