How to replace particular character in string with tcl script? - string

set some_string "Name/is/ComplexSTRUCTUre"
convert this string to,
some_string = "Name/is/ComplexSTR.CTUre"
i.e replacing first "U" to "."

Try This,
set replaced_string [regsub "U" $some_string "."]
puts $replaced_string
Another Option,
set pos [string first "U" $some_string]
set replaced_string [string replace $some_string $pos $pos "."]
puts $replaced_string
Here your "Name/is" portion should not contain any "U"
More information can be found here tcl string replacement

Use of regsub is overkill for plain strings. If you simply wish to replace one set of substrings with another, string map is your friend:
set s "Name/is/ComplexSTRUCTUre"
set s [string map {U .} $s]
This, however, will replace all Us with dots -- as your question's title suggests.
If, however, you want only the first U replaced -- as the text of your question implies, then AxT_8041's second option is the most suitable.

you can use:
string replace <num1> <num2> <To_be_replaced>
example:
set a [get_attr [get_cells -filter <cell_name> ] ref_name]
<cell_name>
string replace 5 8 NAME
<cell_NAME>

Related

java String.format - how to put a space between two characters

I am searching for a way to use a formatter to put a space between two characters. i thought it would be easy with a string formatter.
here is what i am trying to accomplish:
given: "AB" it will produce "A B"
Here is what i have tried so far:
"AB".format("%#s")
but this keep returning "AB" i want "A B". i thought the number sign could be used for space.
i also tried this:
"26".format("%#d") but its still prints "26"
is there anyway to do this with string.formatter.
It is kind of possible with the string formatter although not directly with a pattern.
jshell> String.format("%1$c %2$c", "AB".chars().boxed().toArray())
$10 ==> "A B"
We need to turn the string into an object array so it can be passed in as varargs and the formatter pattern can extract characters based on index (1$ and 2$) and format them as characters (c).
A much simpler regex solution is the following which scales to any number of characters:
jshell> "ABC^&*123".replaceAll(".", "$0 ").trim()
$3 ==> "A B C ^ & * 1 2 3"
All single characters are replaced with them-self ($0) followed by a space. Then the last extra space is removed with the trim() call.
I could not find way to do this using String#format. But here is a way to accomplish this using regex replacement:
String input = "AB";
String output = input.replaceAll("(?<=[A-Z])(?=[A-Z])", " ");
System.out.println(output);
The regex pattern (?<=[A-Z])(?=[A-Z]) will match every position in between two capital letters, and interpolate a space at that point. The above script prints:
A B

How to split string using a whole word as separator?

Is there a way to split string like:
"importanttext1 has gathered importanttext2 from stackoverflow."
I want to grab anything before the word "has", and just want to grab the one word after "gathered", so it doesn't include "from stackoverflow.". I want to be left with 2 variables that cointain importanttext1 and importanttext2
local str = "importanttext1 has gathered importanttext2 from stackoverflow."
local s1 = str:match("(.+)has")
local s2 = str:match("gathered%s+(%S+)")
print(s1)
print(s2)
Note that "%s" matches a whitespace character while "%S" matches a non-whitespace character.

Get the length of the string in substitution

I'd like to calculate the length of a replace string used in a substitution. That is, "bar" in :s/foo/bar. Suppose I have access to this command string, I can run and undo it, and may separate the parts marked by / with split(). How would I get the string length of the replace string if it contains special characters like \1, \2 etc or ~?
For instance if I have
:s/\v(foo)|(bars)/\2\rreplace/
the replace length would be strlen("bars\rreplace") = 12.
EDIT: Just to be clear, I hope to use this to move the cursor past the text that was affected by a substitute operation. I'd appreciate alternative solutions as well.
You have to use :help sub-replace-expression. In it, you use submatch(2) instead of \2. If the expression is a custom function, you can as a side effect store the original length in a variable, and access that later:
function! Replace()
let g:replaceLength = strlen(submatch(0))
" Equivalent of \2\rreplace
return submatch(2) . "\r" . 'replace'
endfunction
:s/\v(foo)|(bars)/\=Replace()/

How to display each individual word of a string in MATLAB

How to display each individual word of a string in MATLAB version R2012a? The function strsplit doesn't work in this version. For example 'Hello, here I am'. I want to display every word on a single line.
Each word in a single line means replacing each blank with a new line:
strrep(s,' ',sprintf('\n'))
You can use regexp with the 'split' option:
>> str = 'Hello, here I am';
>> words = regexp(str, '\s+', 'split').'
words =
'Hello,'
'here'
'I'
'am'
Change '\s+' to a more elaborate pattern if needed.

Convert underscores to spaces in Matlab string?

So say I have a string with some underscores like hi_there.
Is there a way to auto-convert that string into "hi there"?
(the original string, by the way, is a variable name that I'm converting into a plot title).
Surprising that no-one has yet mentioned strrep:
>> strrep('string_with_underscores', '_', ' ')
ans =
string with underscores
which should be the official way to do a simple string replacements. For such a simple case, regexprep is overkill: yes, they are Swiss-knifes that can do everything possible, but they come with a long manual. String indexing shown by AndreasH only works for replacing single characters, it cannot do this:
>> s = 'string*-*with*-*funny*-*separators';
>> strrep(s, '*-*', ' ')
ans =
string with funny separators
>> s(s=='*-*') = ' '
Error using ==
Matrix dimensions must agree.
As a bonus, it also works for cell-arrays with strings:
>> strrep({'This_is_a','cell_array_with','strings_with','underscores'},'_',' ')
ans =
'This is a' 'cell array with' 'strings with' 'underscores'
Try this Matlab code for a string variable 's'
s(s=='_') = ' ';
If you ever have to do anything more complicated, say doing a replacement of multiple variable length strings,
s(s == '_') = ' ' will be a huge pain. If your replacement needs ever get more complicated consider using regexprep:
>> regexprep({'hi_there', 'hey_there'}, '_', ' ')
ans =
'hi there' 'hey there'
That being said, in your case #AndreasH.'s solution is the most appropriate and regexprep is overkill.
A more interesting question is why you are passing variables around as strings?
regexprep() may be what you're looking for and is a handy function in general.
regexprep('hi_there','_',' ')
Will take the first argument string, and replace instances of the second argument with the third. In this case it replaces all underscores with a space.
In Matlab strings are vectors, so performing simple string manipulations can be achieved using standard operators e.g. replacing _ with whitespace.
text = 'variable_name';
text(text=='_') = ' '; //replace all occurrences of underscore with whitespace
=> text = variable name
I know this was already answered, however, in my case I was looking for a way to correct plot titles so that I could include a filename (which could have underscores). So, I wanted to print them with the underscores NOT displaying with as subscripts. So, using this great info above, and rather than a space, I escaped the subscript in the substitution.
For example:
% Have the user select a file:
[infile inpath]=uigetfile('*.txt','Get some text file');
figure
% this is a problem for filenames with underscores
title(infile)
% this correctly displays filenames with underscores
title(strrep(infile,'_','\_'))

Resources