String Pattern search and replace with new string pattern - c#-4.0

How to replace all occurrence string pattern except first occurrence in long text using C sharp. Please help me with one example

Giving an example. Please try to search for a solution on internet first. If no direct solution could be found, then please ask in this forum.
Not the exact code of C# but consider it as a pseudo code. There are many ways to do it.
// First method
int first_index_of = s.find(input_String);
String temp = input_String.substr(first_index_of, input_String.length);
temp = temp.replace("Old", "New");
input_String = input_String.substring(0,first_index_of) + temp;
//Second Method
int i = 0;
while ( didn't reach end of string ){
find next occurrence starting from index i
// Ignore the first occurrence and Replace all other occurrences
if ( Ctr >1)
replace the string
// Exit if no more sub strings found
}
These are just pseudo codes. i expect you to do some work on it prior you use it.

Related

How do I find unique characters in users input?

This algorithm creates a string by taking each unique character in the message in the order they first appear and putting that letter and the number of times it appears in the original message into the shortened string. Your algorithm should ignore any spaces in the message, and any characters which it has already put into the shortened string. For example, the string "I will arrive in Mississippi really soon" becomes "8i1w4l2a3r1v2e2n1m5s2p1y2o".
Here's my code for determining how many unique characters there are. I'm having trouble creating the nested loop to scan the whole string. Help pls!!
boolean used = false;
for (int j = 0; j<i; j++){
if (input.substring(j,j+1).equals(ltr)){
used = true;
}
}
if (!used){
num++;
int count = 0;
for(int k=i; k<input.length(); k++){
if(input.substring(k,k+1).equals(ltr))
count++;
}
}
I am not sure about that. Maybe your nested loop is not right.
Do you use nested loop?
your code is like this: for(){} for(){}
not for(){ for(){ }}
your program just scan the current character and the next character in position ! to find it is unique or not that's the problem
here your problem exactly
if (input.substring(j,j+1).equals(ltr)){

Finding and comparing a range of characters in one string to another string

I would like to get the index and value of a string that matches another string. Example:
buff = '01101010'
mystr = '100010101101010000001'
I want to search for buff in mystr and if it matches then get the index of the first element that matches and the values.
output:
match happens at:
index = 7
value = '01101010'
any help will be appreciated. Thanks.
You could just use find() on the string you're trying to match:
print(mystr.find(buff)) // prints 7
Alternatively you can use a regex:
result = re.search(buff, mystr)
print(result.start()) // prints 7
print(result.group()) // prints 01101010
Building a function around that to get the expected output should be trivial.

Lua: delete specific characters in a string

I have a string that includes all the characters which should be
deleted in a given string. With a nested loop I can iterate through
both strings. But is there a shorter way?
local ignore = "'`'"
function ignoreLetters( c )
local new = ""
for cOrig in string.gmatch(c,".") do
local addChar = 1
for cIgnore in string.gmatch(ignore,".") do
if cOrig == cIgnore then
addChar = 0
break -- no other char possible
end
end
if addChar>0 then new = new..cOrig end
end
return new
end
print(ignoreLetters("'s-Hertogenbosch"))
print(ignoreLetters("'s-Hertogen`bosch"))
The string ignore can also be a table if it makes the code shorter.
You can use string.gsub to replace any occurance of a given string in a string by another string. To delete the unwanted characters, simply replace them with an empty string.
local ignore = "'`'"
function ignoreLetters( c )
return (c:gsub("["..ignore.."]+", ""))
end
print(ignoreLetters("'s-Hertogenbosch"))
print(ignoreLetters("'s-Hertogen`bosch"))
Just be aware that in case you want to ignore magic characters you'll have to escape them in your pattern.
But I guess this will give you a starting point and leave you plenty of own work to perfect.

How to detect string end position in mql4?

Im trying to figure out how to find the end of a string. I'm able to get to the first number in the string that i want (the 6), but I cant figure out how count to the end of the string </td>.My thought is if i can figure how how far it is from the 6 to the closing bracket,I can then substring to get the number 6586.97. Is that correct thinking?
example string <td>6586.97 Lots</td>
Daniel, thank you for your reply. This is what i love about coding. There are a number of ways to get the job done. I had come up with a solution late last night and will post it here.
Count=0;
for (int i =0 ; i<20; i++){
VOL1=StringSubstr(data,string_pos+112+i+string_length,1);
if(VOL1!="/")Count++;
else{Answer=StringSubstr(data,string_pos+112+string_length,Count-6);}}
return(Answer);
What i did was, starting at the 6 i just kept moving to the right till i found the forward slash. Then StringSubstr back.
Use StringFind() to detect start of the message that starts with ">" and end of the message (space bar) and do not forget to start looking for end after start. StringSubstr() is to cut the string that you need
string getStringFromTag(const string example){
int starts = StringFind(example,">");
if(starts>0){
int ends = StringFind(example," ",starts+1);
if(ends>0){
string result=StringSubstr(starts+1,ends-starts-1);
return result;
}
}
return NULL;}

Remove part of string (regular expressions)

I am a beginner in programming. I have a string for example "test:1" and "test:2". And I want to remove ":1" and ":2" (including :). How can I do it using regular expression?
Hi andrew it's pretty easy. Think of a string as if it is an array of chars (letters) cause it actually IS. If the part of the string you want to delete is allways at the end of the string and allways the same length it goes like this:
var exampleString = 'test:1';
exampleString.length -= 2;
Thats it you just deleted the last two values(letters) of the string(charArray)
If you cant be shure it's allways at the end or the amount of chars to delete you'd to use the version of szymon
There are at least a few ways to do it with Groovy. If you want to stick to regular expression, you can apply expression ^([^:]+) (which means all characters from the beginning of the string until reaching :) to a StringGroovyMethods.find(regexp) method, e.g.
def str = "test:1".find(/^([^:]+)/)
assert str == 'test'
Alternatively you can use good old String.split(String delimiter) method:
def str = "test:1".split(':')[0]
assert str == 'test'

Resources