Thread Subject: How to unformat a formatted string, remove new line

Subject: How to unformat a formatted string, remove new line

From: galdoor galdos

Date: 6 Nov, 2009 22:47:01

Message: 1 of 2

I'm trying to figure out the relationship between strings and formatted strings when it comes to escape characters. I have a string with a \n new line in it but I can't easily remove the new line with the string replace function (strrep). Of course I can tokenize and reconstruct the string but is there any way to convert a formatted string variable like myFormattedString back to myString as shown below?

>> myString = 'foo\nbar'
myString =

foo\nbar
>> myFormattedString = sprintf('%s',myString)
myFormattedString =

foo\nbar
>> myFormattedString = sprintf(myString)
myFormattedString =

foo
bar
>> strrep(myFormattedString, 'foo','bar')
ans =

bar
bar
>> strrep(myFormattedString, '\n','bar')
ans =

foo
bar %why was the new line not removed???


thanks,
Mike

Subject: How to unformat a formatted string, remove new line

From: Steven Lord

Date: 8 Nov, 2009 23:58:11

Message: 2 of 2


"galdoor galdos" <mikesmaps@gmail.com> wrote in message
news:hd2915$8ki$1@fred.mathworks.com...
> I'm trying to figure out the relationship between strings and formatted
> strings when it comes to escape characters. I have a string with a \n new
> line in it but I can't easily remove the new line with the string replace
> function (strrep). Of course I can tokenize and reconstruct the string
> but is there any way to convert a formatted string variable like
> myFormattedString back to myString as shown below?

*snip*

>>> strrep(myFormattedString, '\n','bar')
> ans =
>
> foo
> bar %why was the new line not removed???

Because once you've SPRINTFed the string, the literal string \n is replaced
with the newline character. In order to replace the newline character, use
it in your STRREP call.

myString = 'foo\nbar'
myFormattedString = sprintf(myString)
newline = sprintf('\n')
strrep(myFormattedString, newline, 'bar')

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
string galdoor galdos 6 Nov, 2009 17:49:05
formatted string galdoor galdos 6 Nov, 2009 17:49:05
escape character galdoor galdos 6 Nov, 2009 17:49:05
n galdoor galdos 6 Nov, 2009 17:49:05
strrep galdoor galdos 6 Nov, 2009 17:49:05
sprintf galdoor galdos 6 Nov, 2009 17:49:05
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com