How to replace a string at specific line number in a text file?

조회 수: 7 (최근 30일)
raju patel
raju patel 2020년 1월 13일
댓글: Rik 2020년 1월 31일
%%.text file contain%%%
In this
part a
simple
method for
floating‐point
to fixed‐point
%%%new .text file contain%%
In this
part a
simple
method for
/////newly added string///
to fixed‐point
how to replace the string at specific line number?
  댓글 수: 3
raju patel
raju patel 2020년 1월 13일
I have updated the question.sorry for that.
Rik
Rik 2020년 1월 31일
Adding the names of frequent contributors as tags only proves you have seen several questions, which means you should know how to write a complete question. It generally does not attract actual attention.

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

답변 (1개)

Andrew Janke
Andrew Janke 2020년 1월 31일
Try this:
  • Read in the whole text file
  • Split it on \r?\n to break it in to lines
  • Now you have an array of lines, so you can edit line(i) to modify just the i-th line
  • Use strrep or regexprep to modify the contents of that line
  • Use strjoin() to join all the lines back together into a single string
  • Write that string out to your new file

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by