Replace hex character in a string with another hex character
Hi Guys,
Heres a problem scenario, hex character 92 looks very much like hex character 27 (apostrophe).
I need to write a program that can replace all hex character 92 to hex 27 in a string.
Being a novice on the regular expressions, I would appreciate if somebody can show
the exact syntax to to perform this replacement.
Many Thanks
-Anil
I don't think you need to use regex to do this:
String s = something();
char a = 0x27;
char b = 0x92;
s.replace(a,b);
And don't forget that since Strings are immutable you need:
s = s.replace(a,b);to do anything useful.
Similar Messages
-
Replacing a special character in a string with another string
Hi
I need to replace a special character in a string with another string.
Say there is a string - "abc's def's are alphabets"
and i need to replace all the ' (apostrophe) with &apos& ..which should look like as below
"abc&apos&s def&apos&s are alphabets" .
Kindly let me know how this requirement can be met.
Regards
SukumariREPLACE
Syntax Forms
Pattern-based replacement
1. REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF]
pattern
IN [section_of] dobj WITH new
[IN {BYTE|CHARACTER} MODE]
[{RESPECTING|IGNORING} CASE]
[REPLACEMENT COUNT rcnt]
{ {[REPLACEMENT OFFSET roff]
[REPLACEMENT LENGTH rlen]}
| [RESULTS result_tab|result_wa] }.
Position-based replacement
2. REPLACE SECTION [OFFSET off] [LENGTH len] OF dobj WITH new
[IN {BYTE|CHARACTER} MODE].
Effect
This statement replaces characters or bytes of the variable dobj by characters or bytes of the data object new. Here, position-based and pattern-based replacement are possible.
When the replacement is executed, an interim result without a length limit is implicitly generated and the interim result is transferred to the data object dobj. If the length of the interim result is longer than the length of dobj, the data is cut off on the right in the case of data objects of fixed length. If the length of the interim result is shorter than the length of dobj, data objects of fixed length are filled to the right with blanks or hexadecimal zeroes. Data objects of variable length are adjusted. If data is cut off to the right when the interim result is assigned, sy-subrc is set to 2.
In the case of character string processing, the closing spaces are taken into account for data objects dobj of fixed length; they are not taken into account in the case of new.
System fields
sy-subrc Meaning
0 The specified section or subsequence was replaced by the content of new and the result is available in full in dobj.
2 The specified section or subsequence was replaced in dobj by the contents of new and the result of the replacement was cut off to the right.
4 The subsequence in sub_string was not found in dobj in the pattern-based search.
8 The data objects sub_string and new contain double-byte characters that cannot be interpreted.
Note
These forms of the statement REPLACE replace the following obsolete form:
REPLACE sub_string WITH
Syntax
REPLACE sub_string WITH new INTO dobj
[IN {BYTE|CHARACTER} MODE]
[LENGTH len].
Extras:
1. ... IN {BYTE|CHARACTER} MODE
2. ... LENGTH len
Effect
This statement searches through a byte string or character string dobj for the subsequence specified in sub_string and replaces the first byte or character string in dobj that matches sub_string with the contents of the data object new.
The memory areas of sub_string and new must not overlap, otherwise the result is undefined. If sub_string is an empty string, the point before the first character or byte of the search area is found and the content of new is inserted before the first character.
During character string processing, the closing blank is considered for data objects dobj, sub_string and new of type c, d, n or t.
System Fields
sy-subrc Meaning
0 The subsequence in sub_string was replaced in the target field dobj with the content of new.
4 The subsequence in sub_string could not be replaced in the target field dobj with the contents of new.
Note
This variant of the statement REPLACE will be replaced, beginning with Release 6.10, with a new variant.
Addition 1
... IN {BYTE|CHARACTER} MODE
Effect
The optional addition IN {BYTE|CHARACTER} MODE determines whether byte or character string processing will be executed. If the addition is not specified, character string processing is executed. Depending on the processing type, the data objects sub_string, new, and dobj must be byte or character type.
Addition 2
... LENGTH len
Effect
If the addition LENGTH is not specified, all the data objects involved are evaluated in their entire length. If the addition LENGTH is specified, only the first len bytes or characters of sub_string are used for the search. For len, a data object of the type i is expected.
If the length of the interim result is longer than the length of dobj, data objects of fixed length will be cut off to the right. If the length of the interim result is shorter than the length of dobj, data objects of fixed length are filled to the right with blanks or with hexadecimal 0. Data objects of variable length are adapted.
Example
After the replacements, text1 contains the complete content "I should know that you know", while text2 has the cut-off content "I should know that".
DATA: text1 TYPE string VALUE 'I know you know',
text2(18) TYPE c LENGTH 18 VALUE 'I know you know',
sub_string TYPE string VALUE 'know',
new TYPE string VALUE 'should know that'.
REPLACE sub_string WITH new INTO text1.
REPLACE sub_string WITH new INTO text2. -
Replaces all occurnces of one String with another String in a given source
Hi all I had found that there is a lot of interest int this topic. there is a little piece of code that I wrote for myself recently, so I fgigured that it might be helpful for some...
This method will scan through a string "source", find all of the occurances of a String "before" and replace them with the "after".
Enjoy :-)
public static String replace(String source, String before, String after)
StringBuffer sb = new StringBuffer(source);
int startpos = source.indexOf(before);
int endpos = startpos + before.length();
int i = startpos;
while( i > -1 )
if( startpos > -1 && endpos <= source.length() )
source = sb.replace(startpos, endpos, after).toString();
int lastReplace = source.lastIndexOf(after) + after.length();
if(lastReplace <= source.length())
startpos = source.substring(lastReplace, source.length() ).indexOf(before);
endpos = startpos + before.length();
i = startpos;
else
i = source.length();
else
i = -1;
return source;
Oh yeah if you want, you can visit my site http://www.infobrokery.com it is where this code is used to replace the text URL with html.Since 1.4 the String class has a replaceAll method ...
-
Stepping through a query result set, replacing one string with another.
I want to write a function that replaces the occurance of a string with another different string. I need it to be a CF fuction that is callable from another CF function. I want to "hand" this function an SQL statement (a string) like this: (Please note, don't bother commenting that "there are eaiser ways to write this SQL..., I've made this simple example to get to the point where I need help. I have to use a "sub_optimal" SQL syntax just to demonstrate the situation)
Here is the string I want to pass to the function:
SELECT
[VERYLONGTABLENAME].FIRST_NAME,
[VERYLONGTABLENAME].LAST_NAME,
[VERYLONGTABLENAME].ADDRESSS
FROM
LONGTABLENAME [VERYLONGTABLENAME]
Here is the contents of the ABRV table:
TBL_NM, ABRV <!--- Header row--->
VERYLONGTABLENAME, VLTN
SOMEWHATLONGTALBENAME, SLTN
MYTABLENAME, MTN
ATABLENAME, ATN
The function will return the original string, but with the abreviations in place of the long table names, example:
SELECT
VLTN.FIRST_NAME,
VLTN.LAST_NAME,
VLTN.ADDRESSS
FROM
LONGTABLENAME VLTN
Notice that only the table names surrounded by brackets and that match a value in the ABRV table have been replaced. The LONGTABLENAME immediately following the FROM is left as is.
Now, here is my dum amatuer attempt at writing said function: Please look at the comment lines for where I need help.
<cffunction name="AbrvTblNms" output="false" access="remote" returntype="string" >
<cfargument name="txt" type="string" required="true" />
<cfset var qAbrvs=""> <!--- variable to hold the query results --->
<cfset var output_str="#txt#"> <!--- I'm creating a local variable so I can manipulate the data handed in by the TXT parameter. Is this necessary or can I just use the txt parameter? --->
<cfquery name="qAbrvs" datasource="cfBAA_odbc" result="rsltAbrvs">
SELECT TBL_NM, ABRV FROM BAA_TBL_ABRV ORDER BY 1
</cfquery>
<!--- I'm assuming that at this point the query has run and there are records in the result set --->
<cfloop index="idx_str" list="#qAbrvs#"> <!--- Is this correct? I think not. --->
<cfset output_str = Replace(output_str, "#idx_str#", ) <!--- Is this correct? I think not. --->
</cfloop> <!--- What am I looping on? What is the index? How do I do the string replacement? --->
<!--- The chunck below is a parital listing from my Delphi Object Pascal function that does the same thing
I need to know how to write this part in CF9
while not Eof do
begin
s := StringReplace(s, '[' +FieldByName('TBL_NM').AsString + ']', FieldByName('ABRV').AsString, [rfReplaceAll]);
Next;
end;
--->
<cfreturn output_txt>
</cffunction>
I'm mainly struggling with syntax here. I know what I want to happen, I know how to make it happen in another programming language, just not CF9. Thanks for any help you can provide.RedOctober57 wrote:...
Thanks for any help you can provide.
One:
<cfset var output_str="#txt#"> <!--- I'm creating a local
variable so I can manipulate the data handed in by the TXT parameter.
Is this necessary or can I just use the txt parameter? --->
No you do not need to create a local variable that is a copy of the arguments variable as the arguments scope is already local to the function, but you do not properly reference the arguments scope, so you leave yourself open to using a 'txt' variable in another scope. Thus the better practice would be to reference "arguments.txt" where you need to.
Two:
I know what I want to happen, I know how to make it happen in another programming language, just not CF9.
Then a better start would be to descirbe what you want to happen and give a simple example in the other programming language. Most of us are muti-lingual and can parse out clear and clean code in just about any syntax.
Three:
<cfloop index="idx_str" list="#qAbrvs#"> <!--- Is this correct? I think not. --->
I think you want to be looping over your "qAbrvs" record set returned by your earlier query, maybe.
<cfloop query="qAbrvs">
Four:
<cfset output_str = Replace(output_str, "#idx_str#", ) <!--- Is this correct? I think not. --->
Continuing on that assumption I would guess you want to replace each instance of the long string with the short string form that record set.
<cfset output_str = Replace(output_str,qAbrs.TBLNM,qAbrs.ABRV,"ALL")>
Five:
</cfloop> <!--- What am I looping on? What is the index? How do I do the string replacement? --->
If this is true, then you are looping over the record set of tablenames and abreviations that you want to replace in the string. -
How to replace a character in a string with blank space.
Hi,
How to replace a character in a string with blank space.
Note:
I have to change string CL_DS_1===========CM01 to CL_DS_1 CM01.
i.e) I have to replace '=' with ' '.
I have already tried with <b>REPLACE ALL OCCURRENCES OF '=' IN temp_fill_string WITH ' '</b>
Its not working.Hi,
Try with this..
call method textedit- >replace_all
exporting
case_sensitive_mode = case_sensitive_mode
replace_string = replace_string
search_string = search_string
whole_word_mode = whole_word_mode
changing
counter = counter
exceptions
error_cntl_call_method = 1
invalid_parameter = 2.
<b>Parameters</b> <b> Description</b> <b> Possible values</b>
case_sensitive_mode Upper-/lowercase false Do not observe (default value)
true Observe
replace_string Text to replace the
occurrences of
SEARCH_STRING
search_string Text to be replaced
whole_word_mode Only replace whole words false Find whole words and
parts of words (default
value)
true Only find whole words
counter Return value specifying how
many times the search string
was replaced
Regards,
Jayaram... -
Replace a string with another string in a file
I have to replace a string with another string. Say for example in a html file it got a line like,
<a href="##"></a>
now i want to replace this ## with the full derived path like
"c;\\sample folder\\sample subfolder\\samplefile.html"
can someone help me to do this?
pls tell me from the file opening till closing the file.I have to replace a string with another string. Say
for example in a html file it got a line like,
<a href="##"></a>
now i want to replace this ## with the full derived
path like
"c;\\sample folder\\samplesubfolder\\samplefile.html"
can someone help me to do this?
pls tell me from the file opening till closing the
file.
public class Buckel {
final static String CONST = "c:\\sample folder\\samplesubfolder\\samplefile.html";
public Buckel() {
public static void main ( String[] argv ) throws Exception {
int idx = 0;
String in = "<a href=\"##\"></a>", // We'll imagine this just gets here somehow. If it's on the i/p file then take the '\' out B4 and aft the '##'.
out = "";
idx = in.indexOf( "##" );
if ( idx > 0 ) {
out = in.substring( 0, idx ) + CONST + in.substring( idx+=2, in.length() );
System.out.println( out );
} -
Replacing a part of a String with a new String
Hi everybody,
is there a option or a method to replace a part of a String with a String???
I only found the method "replace", but with this method I only can replace a char of the String. I don't need to replace only a char of a String, I have to replace a part of a String.
e.g.:
String str = "Hello you nice world!";
str.replace("nice","wonderfull"); // this won't work, because I can't replace a String with the method "replace"
// with this method I'm only able to replace charsDoes anyone know some method like I need???
Thanks for your time on answering my question!!
king regards
IceCube-Ddo check java 1.4 api, I think there is a method in it, however for jdk1.3 you can use
private static String replace(String str, String word,String word2) {
if(str==null || word==null || word2 == null ||
word.equals("") || word2.equals("") || str.equals("")) {
return str;
StringBuffer buff = new StringBuffer(str);
int lastPosition = 0;
while(lastPosition>-1) {
int startIndex = str.indexOf(word,lastPosition);
if(startIndex==-1) {
break;
int len = word.length();
buff.delete(startIndex,startIndex+len);
char[] charArray = word2.toCharArray();
buff.insert(startIndex,charArray);
str = buff.toString();
int len2 = startIndex+word2.length();
lastPosition = str.indexOf(word,len2);
return buff.toString(); -
Replace multiple whitespaces in a string with single one
Hi,
I'm using Oracle 9.2.0 and I have a query on string manipulation.
Can I replace multiple whitespaces within a string with single one. I tried with regexp_replace function but it is not supporting Oracle 9i.
Can someone help me out?
Regards,
PramodJust playing around:
WITH
Strings
AS
SELECT 'Can I replace multiple whitespaces within a string with single one' Text FROM Dual
SELECT
REPLACE
REPLACE
XMLAGG
XMLElement
"A",
SubStr(Text, Level, 1)
'</A>'
'<A>'
) Text
FROM
Strings
WHERE
NOT
Level > 1
AND SubStr(Text, Level, 1) = ' '
AND SubStr(Text, Level - 1, 1) = ' '
CONNECT BY
Level <= LENGTH(Text); -
Replacing a character in a string to another character
hi,
i need to write a function or procedure to replace the character of a string value suppose:
l_error:= 'abcdefghijklmnop' is a string
i need write a function or procedure to replace the character "c" to "z"
that data in l_error is not in any table.
thanks,
AJI want to Replace all the Existence of the word - "Test" in a string with "Test1" whereever a space exits before the word Test and someother alphabet after "Test" i.e. Test will be replaced with Test1 if a word starts with Test and contains more alphabets also. For example - TestName should be replaced with Test1Name while MyTest should not be updated to MyTest1.
I have tried to use below query which uses oracle regular expressions -
SELECT REGEXP_REPLACE('MYCOMPANY TEST TESTGEET INDIA PVT LTD TEST','\s(TEST)\w',' TEST1') FROM DUAL
Output -
"MYCOMPANY TEST *TEST1EET* INDIA PVT LTD TEST"
Here, it has also replaced the G also from TESTGEET and resulted in TEST1EET while i want TEST1GEET.
Can someone please suggest how can i do this..... may b m doing some silly mistake but sorry m a newbie to regular expression...
Thanks in advance.. -
Replacing all occurrences of characters in a string with one new character?
Hi there,
I'm looking for a way that I could replace all occurrences of a set of specific characters with just one new character for each occurrence.
For example if I have a string which is "word1...word2.......word3....word4............word5" how would I be able to replace this with just one character such as ":" for each set of "." so that it would essentially appear like this "word1:word2:word3:word4:word5"
If I just use replace(".", ":") I am left with "word1:::word2:::::::word4::::word5::::::::::::"
So therefore I'm guessing this would require the use of replaceAll() maybe? but I'm not really very familiar with how regular expressions work and how this would be accomplished using them.
Any help would be greatly appreciated :) Thanks.Yes, but "." means any character, so ".\+" means "any character repeated more than once". If you just mean a full stop ("period" for you Americans :p) you should use "\.+" as your regular expression, but remember that for Java you need a '\' escape to recognise '\' as '\', so in code you'd actually type your regex as:
"\\.+"Here's an example of one way to do it:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String string = "example1.......example2...example3.....example4";
Pattern pattern = Pattern.compile("\\.+");
Matcher stringMatcher = pattern.matcher(string);
string = stringMatcher.replaceAll(":");
System.out.println(string);
}Edited by: JohnGraham on Oct 24, 2008 5:19 AM
Edited by: JohnGraham on Oct 24, 2008 5:22 AM -
How can I substitute a string with another string in a file
I have a file. I have a substitute a keyword with another string in all the occurences of a file. How can I do this?
I'm gonna give you the benifit of the doubt and assume you didn't mean to double post your question.
As to substitute a keyword with a string one way is to read in the file and run it through a StreamTokenizer class to break it into words. Pull one word at a time, check it, and stick it into a StringBuffer. Once your done with the file overwrite it with what is in the StringBuffer.
Another way might be to use the RandomAccessFile class, but I'm not really familiar with that because I hardly ever use it. -
I just replaced my ipod touch 2nd gen with another how can i get my itunes back
i just replaced my ipod touch 2nd gen with the same model and cant seem to get my iyunes back
Don't understand question, Changing iPod shouldn't affect iTunes on the computer you sync with. Plug in new iPod, it should take you through some screens of options asking how you want to setup the new iPod.
Just did this when going from 7+ year old iPod video to iPod Touch 5th gen. -
How do I replace one JPanel on a JFrame with another?
I want to replace one JPanel on a JFrame with a different JPanel when the user clicks on a certain menu item. The menu item is working corrrectly but I cant seem to repaint / refresh the component.
My code is as follows:
c.add(knotPanel, BorderLayout.CENTER);
c.add(stepPanel, BorderLayout.NORTH);
if (stepChoice != 9) {
c.remove(ownPanel);
c.add(lessonPanel, BorderLayout.EAST);
else {
c.remove(lessonPanel);
c.add(ownPanel, BorderLayout.EAST);
c.invalidate();
c.validate();
c.update(c.getGraphics());
Any ideas?I think you should use CardLayout manager.
With CardLayout you can switch beetwen JPanels without removing or adding. -
Replace the first character in a String with blank
String a = dgramRsp.getAddress().toString();
a = a.replace('/','');The problem with the first statement is that it returns an ip address with a '/' in front of it. Like so.../192.168.0.190
I need to get rid of the '/'. The replace won't work because it wants a character between the ' '. Thanks for your time.I had to think about it for a sec but yes you're
right.
InetAddress a = dgramRsp.getAddress();
String b = a.getHostAddress();
substring(1) would have worked, too, but it would have been redundant (removing a previously added slash). -
How to replace part of a String with another String :s ?
Got a String
" Team_1/Team_2/Team_3/ "
And I want to be able to rename part of the string (they are seperated by '/'), for example I want to rename "Team_3" to "Team C", and "Team_1" to "Team A" while retaining the '/' character how would I do this?
I have tried using String.replace(oldValue, newValue); but this doesnt work, any ideas?What do you mean that it doesn't work? You do know that the method returns the modified string? The actual string that you invoke the method on is not altered.
/Kaj
Maybe you are looking for
-
Can I keep my itunes library on an external hard drive and still use it??
Hello, I recently purchased a new laptop because my old computer wwent on the fritz. Luckily I backed up my library on my external hard drive. My question is can a) connect my itouch and ihone to my nex computer and b) run my library strictly from m
-
Error Occurred During the import
Hi friends, When i tried to import the sql file of my already build apex application into my work space, and at the time of installation it is returning error like Error ERR-1029 Unable to store session info. session=1798024046224326 item=4000685
-
JCA IConnection call to BAPI with table in table as import and export param
Is it possible to call a BAPI with a table in table as import and export data? Please give small code example if this is possible. Thanks in advance.
-
Workflow WS00000038 Purch.Req - Event REJECTED not triggered
Hi Friends! (SAP ECC 6.0) (Tcode SWE3 - BUS2009-REJECTED is active) When using WS00000038 Purchase requisition we have problem when an item is REJECTED. the Event that is triggered is BUS2009-SIGNIFICANTLYCHANGED. This leads to that the Workflow end
-
Unable to reinstall Photoshop CS2 from Creative Suite
I had to uninstall Creative Suite CS2 because I wanted to try CS5. Come to find out after a lot of frustration unable to do because I have a G5 that does not have an intell processor. When I tried to reload the Creative Suite CS2 the Photoshop