Remove a substring/ string pattern of a string in Erlang - functional-programming

I have an xml string like
S = "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3/></B>".
I want to remove the end tag </B>
S2 = "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3/>"
How can I achieve this?

If you only want to remove the specific string literal </B> then getting a sublist will do the trick:
S = "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3\"/></B>",
lists:sublist(S, 1, length(S) - 4).
%%= "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3\"/>"
If you need a more general approach you can use the re:replace/3 function:
S1 = re:replace(S, "</B>", ""),
S2 = iolist_to_binary(S1),
binary_to_list(S2).
%%= "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3\"/>"
Update
As mentioned in the comments, providing the option {return, list} is much cleaner:
re:replace(S, "</B>", "", [{return,list}]).
%%= "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3\"/>"

You can use string:replace/3 function from string module.
string:replace/3 - replaces SearchPattern in String with Replacement. 3rd function parameter indicates whether the leading, the trailing or all encounters of SearchPattern are to be replaced.
S2 = string:replace(S, "</B>", "", all).

Related

Regex is not working for particular string

I have to find these string using regex:-
(APP12345-85)
(APP12345XDP-85)
(APP12345X-85)
(APP12345-85) - not working for this one
(APP12345) - not working for this one
the original text is like this
.......some text 123 (APP12345-85) some text...............
My code is:-
Regex rgx = new Regex(#"(APP|REG)[0-9]{5}[A-Z]{5}-[0-9]{2}", caseIgnore);
MatchCollection matches = rgx.Matches(#evalString);
if (matches.Count > 0)
{
//code
}
Any help will be appreciated.
You can also match these entries with
\b(APP|REG)[0-9]{5}[A-Z]{0,5}(?:-[0-9]{2})?\b
Looks like the uppercase letters are optional, so setting to {0,5} looks safe.
And this regex does not check the beginning/end of the string/line.
See demo.
UPDATE:
Here is a sample code for the updated example:
Regex rgx = new Regex(#"\((APP|REG)[0-9]{5}[A-Z]{0,5}(?:-[0-9]{2})?\)", RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches("(APP12345-85)");
if (matches.Count > 0)
{
//code
}
Output of matches:

Fetching content of multiline textbox using Visual Studio

TextBox txtContent = new TextBox();
SPList announcementList = mySite.Lists["Announcements"];
SPListItem getAnnouncement = announcementList.Items[0];
txtContent.Text = getAnnouncement["Body"].ToString();
this gives output as
<div class="ExternalClass61EB4AB2F639401D9141EADFC30FEDFE">
<p>Please follow plan of action.​</p>
</div>
I want output as
"Please follow plan of action."
Please Guide.
Use SPFieldMultiLineText like the following:
SPListItem getAnnouncement = announcementList.Items[0];
SPFieldMultiLineText bodyField = getAnnouncement.Fields.GetField("Body") as SPFieldMultiLineText;
string txt = bodyField.GetFieldValueAsText(getAnnouncement["Body"]);
string html = bodyField.GetFieldValueAsHtml(getAnnouncement["Body"]);
If you're looking to remove the HTML from the content, this answer will help you:
How can I strip HTML tags from a string in ASP.NET?
There are a number of solutions there, such as the one with Regex. You would just need this line of code:
txtContent.Text=WebUtility.HtmlDecode(Regex.Replace(getAnnouncement["Body"].ToString(), "<[^>]*(>|$)", string.Empty))
SPHttpUtility.ConvertSimpleHtmlToText(text, text.Length - 1);

Trim function not removing spaces in name

bool Res = false;
DataView DV = new DataView(DT);
DV.RowFilter = "Trim(Originator)='"+OrginatorName.Trim()+"'";
if (DV.Count > 0)
{
Res = true;
}
I need to get "Originator" from the database and compare it with the OrginatorName to check duplicate values. I need to remove all the white spaces before checking.
For example, the function must consider "John Van" to be the same as "JohnVan". My above code doesn't work. How can I achieve this?
String.Trim() removes whitespace from the beginning and end only, not in the middle. You want to use the String.Replace() method
DV.RowFilter = "Trim(Originator)='"+OrginatorName.Replace(" ", "")+"'";
this line should be
DV.RowFilter = "Trim(Originator)='"+OrginatorName.Replace(" ","")+"'";
User .Replace instead of .Trim()

How to remove part of the string starting from special char (or word) in ActionScript?

so I have a string "bla dla dla vre bla 54312" I want to turn it into "bla dla dla " by saying something like
function(string, "vre");
how to do such thing?
I'm sure you know how to write the function around this, but this is all you really need to do what you're asking. Check out the liveDocs for details about the subString and indexOf methods.
var newString:String;
newString = "bla dla dla vre bla 54312"
newString = newString.subString(0,newString.indexOf("vre"));
This might get you started:
var s:String = "bla dla dla vre bla 54312";
var a:Array = s.split("vre");
if(a) {
// a[0] should be 'bla dla dla'
trace(a[0]);
}
I'm not familiar with actionscript syntax, but this seems like it'd be fairly easy. You could try:
function trimStr(myStr, searchStr)
{
var index:Int = myStr.search(searchStr);
if (index > -1)
{
return myStr.substring(0, index);
}
else
{
return myStr;
}
}
I may've gotten some syntax wrong, but the basic concept still comes through.

remove last two string char!

The following code:
If checkboxList.Items(i).Selected Then
.Fields("DESC1").Value += checkboxList.Items(i).Text + ", "
End If
should produce output such as "A, B, C,(space)", which will then be bound to a dynamically created GridView. I would like to remove the last two-char string, that is ",(space)". How can I do this?
Take a look at String.Join, which may do what you want, without the need to manipulate the final two characters.
I wouldn't add them on in the first place :) try
If checkboxList.Items(i).Selected Then
if .Fields("DESC1").Value Is System.DbNull.Value then
.Fields("DESC1").Value = checkboxList.Items(i).Text
else
.Fields("DESC1").Value += ", " + checkboxList.Items(i).Text
End If
End If
For info, string concatenation is expensive. It looks (from the i and from the results) like you should really be using a StringBuilder; some rough pseudo-code (in C#, but trivial to translate to VB.Net):
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < checkboxList.Items.Count ; i++) {
if(checkboxList.Items[i].Selected) {
if(sb.Length > 0) sb.Append(", "); // add separator
sb.Append(checkboxList.Items[i].Text); // add text
}
}
someOjb.Fields("DESC1") = sb.ToString(); // use result
You can use .TrimEnd(", ".ToCharArray()) on the string, or you can use SubString:
strLetters.Substring(0, strLetters.Length - 2)
It seems you just want to get "A, B, C" from "A, B, C, ". A bit of simple string manipulation should do the job:
Dim input = "A, B, C, "
Dim result = input.Substring(0, input.LastIndexOf(","))
This is more versatile than simply removing the last two characters, since it looks for the last comma, which is what I believe you are after.
Of course, the fact that you're adding on these two chars in the first place sounds a bit dodgy. I'd need to see more context to show how this can be avoided, however.
use
.Fields("DESC1").Value += checkboxList.Items(i).Text + ", "
. Fields("DESC1").Value = .Fields("DESC1").Value.TrimRight(new []{',',' '});
PS:- sorry if it is not valid vb syntax :)
There is also just "Remove":
string k = "okay";
string s = k.Remove(k.Length - 2, 2);
This will remove all trailing , and/or [space]:
.Fields("DESC1").Value = .Fields("DESC1").Value.TrimRight(", ".ToCharArrray())
var selectedValues = checkboxList.Items
.Where(i => i.Selected)
.Select(i => i.Fields("DESC1").Value);
var result = String.Join(", ", selectedValues);
Does VB have a ternary if operator?
If checkboxList.Items(i).Selected Then
.Fields("DESC1").Value += checkboxList.Items(i).Text + (i == checkboxList.Items.Length-1 ? "" : ", ")
End If

Resources