Smalltalk, how to insert tab in a string - string

How do you insert a "tab" in a string? I thought it was t enclosed in <> , but I do:
'Name <t> Age <t> Occupation'
prints exactly how it's typed. I would like to get
Name Age Occupation
instead of
Name <t> Age <t> Occupation

'Name ', String tab, ' Age ', String tab, ' Occupation'
If String does not understand #tab, you have to :
'Name ', Character tab asString, ' Age ', Character tab asString, ' Occupation'

As Damien noted, you can simply concatenate several calls to String tab to achieve what you wanted. The <t> trick you're trying, though, happens in Squeak-based Smalltalks if you call expandMacros on your string. E.g.,
'Here is<t>a tab and<n>a blank line' expandMacros
This mechanism is generic, and quite easily extensible; see String>>expandMacrosWithArguments: for more information.

Related

SPLIT results with separator

I'm trying to split a string (separated with the HTML break tag), without deleting the break tag. I think it's pretty messy to add a break as string after splitting, so is there any function/possibility to keep the separator while "splitting"?
Example:
<HTML><BODY><p>some text<br/>some more text</p></BODY></HTML>
Expected result:
<HTML><BODY><p>some text<br/>
some more text</p></BODY></HTML>
As far as I know SPLIT removes the separator from the results and it doesn't seem like you can change that.
But you could create your own separator by first replacing your <br/> tag with <br/> plus an arbitrary string that is highly unlikely to ever appear in your HTML source, and then split the HTML using this arbitrary string as a separator instead.
types:
begin of t_result,
segment(2000) type c,
end of t_result.
DATA:
source type string,
separator type string,
brtag type string,
repl type string,
result_tab type standard table of t_result,
result_row TYPE t_result.
brtag = '<br/>'.
separator = '|***SEP***|'.
concatenate brtag separator into repl.
source = '<HTML><BODY><p>some text<br/>some more text</p></BODY></HTML>'.
replace all occurrences of brtag in source with repl.
split source at separator into table result_tab.
LOOP AT result_tab INTO result_row.
WRITE:
result_row-segment.
ENDLOOP.
Output of that example report:
<HTML><BODY><p>some text<br/>
some more text</p></BODY></HTML>
The caveat of this solution is that your custom separator, if not chosen with some care, might appear in your HTML source on its own. I therefore would choose an arbitrary string with a special character or two that would be encoded in HTML (like umlauts) and therefore not appear in your source.
Just use the replace command. replace <br/> with <br/>CR_LF
The CR_LF refers to the carriage return linefeed character.
In more complex cases you can use regex expressions in abap.
class ZTEST_SO definition public create public .
public section.
methods t1.
ENDCLASS.
CLASS ZTEST_SO IMPLEMENTATION.
METHOD T1.
data: my_break type string,
my_string type string
value '<HTML><BODY><p>some text<br/>some more text</p></BODY></HTML>'.
my_break = '<br/>' && CL_ABAP_CHAR_UTILITIES=>CR_LF.
replace all occurrences of '<br/>' in my_string with my_break in character mode.
"check my_string in the debugger :)
"<HTML><BODY><p>some text<br/>
"some more text</p></BODY></HTML>
ENDMETHOD.
ENDCLASS.

python3 replace ' in a string

I am trying to clean text strings containing any ' or &#39 (which includes an ; but if i add it here you will see just ' again. Because the the ANSI is also encoded by stackoverflow. The string content contains ' and when it does there is an error.
when i insert the string to my database i get this error:
psycopg2.ProgrammingError: syntax error at or near "s"
LINE 1: ...tment and has commenced a search for mr. whitnell's
the original string looks like this:
...a search for mr. whitnell&#39s...
To remove the ' and &#39 ; I use:
stripped_content = stringcontent.replace("'","")
stripped_content = stringcontent.replace("&#39 ;","")
any advice is welcome, best regards
When you try to replace("&#39 ;","") it literally searching for "&#39 ;" occurrences in string. You need to convert "&#39 ;" to its character equivalent. Try this:
s = "That's how we 'roll"
r = s.replace(chr(int('&#39'[2:])), "")
and with this chr(int('&#39'[2:])) you'll get ' character.
Output:
Thats how we roll
Note
If you try to run this s.replace(chr(int('&#39'[2:])), "") without saving your result in variable then your original string would not be affected.

Return the quotation marks in return statement

I made a function that returns a string as shown below but I was wondering how do I keep the double quotes for the quote?
def quote_maker(quote, name, year):
''' (string, string, number)-> None
Returns a sentence displaying in what given year the
given name of a person said the given quote.
'''
return (' In '+ str(year) +', a person called '+ name +' said: '+ quote)
For example my function returns:
quote_maker("Everything should be made as simple as possible but not simpler.", "Albert Einstein", 1933)
'In 1933, a person called Albert Einstein said: Everything should be made as simple as possible but not simpler.'
Instead of: (with double quotes)
'In 1933, a person called Albert Einstein said: "Everything should be made as simple as possible but not simpler."'
You can escape characters (done with \) that have a special meaning to treat them as characters:
" this is quoted: \"example\" "
represents the string:
this is quoted: "example"
I think best way is to use string formating. And return the variable.
sentence = 'In {0}, a person called {1} said: "{2}".'.format(year, name, quote)
For complicated string you should use escape characters. For more details enter link description here

Input a string, but the program does not recognize the spaces between words

I would like this to accept "John Smith" or "Smith, John". Currently this only accepts JohnSmith Smith,John etc. What is the issue?
while not str(NAME).isalpha():
NAME=input("Enter patient's name : ")
new_entry.append(str(NAME))
The isalpha method checks if the string is one word, e.g. no numbers and no spaces. For multiple words, you can use something like
while not all(s.isalpha() for s in NAME.replace(',', ' ').split()):
where the .replace(',', ' ') is what allows for treating spaces and commas on the same footing.

Insert words into string starting from specific index

I have string that looks like this : 'Foo dooo kupa trooo bar'.
I know the start and end point of word kupa and I need to wrap it with this : <span> </span>.
After this operation i want my string to like like this : Foo dooo <span>kupa</span> trooo bar
I cannot find any good built-in methods that can help so any help would be nice.
basically there are two options:
import re
strg = 'Foo dooo kupa trooo bar'
match = re.search('kupa', strg)
print('{}<span>{}</span>{}'.format(strg[:match.start()], strg[match.start():match.end()], strg[match.end():]))
or (the first expressioin would be a regex, if the pattern were a little more complicated):
print(re.sub('kupa', '<span>kupa</span>', strg))
The output string and the input string are the same.....
Anyway I think you want to replace part of your string, so have a look at this answers:
https://stackoverflow.com/a/10037749/4827890
https://stackoverflow.com/a/12723785/4827890

Resources