Problem in appending a string to a already filled string builder(at the beginning by using INSERT) and then converting that to string array(C#3.0) - c#-3.0

I have a string builder like
StringBuilder sb = new StringBuilder("Value1");
sb.AppendLine("Value2");
Now I have a string say
string str = "value 0";
I did
sb.Insert(0,str);
and then
string[] strArr = sb.ToString().Trim().Replace("\r", string.Empty).Split('\n');
The result I am getting as (Array size of 2 where I should get 3)
[0] value 0 Value1
[1] value2
But the desired output being
[0] Value 0
[1] Value1
[2] Value2
Where I am going wrong?
I am using C#3.0
Please help.. It 's urgent
Thanks

The method StringBuilder.Insert does not insert a new line automatically so you have to add one yourself:
string str = "value 0" + Environment.NewLine;

Actually, you would get an array of size one. You put "Value1" in the StringBuilder when you create it, then you add "Value2" and a line break, making the string "Value1Value2\r\n" (assuming the CR+LF line break for this example). Then you insert "Value 0" at the beginning, making the string "Value 0Value1Value2\r\n". Trimming the string removes the line break at the end, and splitting on a character that doesn't exist in the string gives you an array with only one item:
[0] Value 0Value1Value2
The Insert method doesn't add a line break like AppendLine does, so you have to add the line break manually:
StringBuilder sb = new StringBuilder();
sb.AppendLine("Value1");
sb.AppendLine("Value2");
string str = "value 0";
sb.Insert(0, str + Environment.NewLine);
Now you can trim and split the string:
string[] strArr =
sb.ToString()
.Trim()
.Split(new string[]{ Environment.NewLine }, StringSplitOptions.None);

You are inserting Value 0 and which would result in the first line being Value0Value1
Insert will only insert a string at the specified position. It does work the same as AppendLine. As there is no carriage return your split won't work as you intended.

Related

Single user defined function that preprocesses a python list of strings

I have the following list of strings
my_list = ["This: is the first string", "This: is another String", This: is the third string of words in the list!"]
I want to create a function that takes each string from my_list in string format and removes the "This: " (the first 6 characters), punctuations, and stop words.
This is what I have tried:
def preprocess(any_list):
[e[6:] for e in any_list]
return any_list
no_punct = [char for char in any_list if char not in string.punctuation]
no_punct = ''.join(no_punct)
clean_words = [word for word in no_punct.split() if word.lower() not in stopwords('english')]
return clean_words
preprocess(my_list)

Swift 5 split string at integer index

It used to be you could use substring to get a portion of a string. That has been deprecated in favor on string index. But I can't seem to make a string index out of integers.
var str = "hellooo"
let newindex = str.index(after: 3)
str = str[newindex...str.endIndex]
No matter what the string is, I want the second 3 characters. So and str would contain "loo". How can I do this?
Drop the first three characters and the get the remaining first three characters
let str = "helloo"
let secondThreeCharacters = String(str.dropFirst(3).prefix(3))
You might add some code to handle the case if there are less than 6 characters in the string

How do I find letters in words that are part of a string and remove them? (List comprehensions with if statements)

I'm trying to remove vowels from a string. Specifically, remove vowels from words that have more than 4 letters.
Here's my thought process:
(1) First, split the string into an array.
(2) Then, loop through the array and identify words that are more than 4 letters.
(3) Third, replace vowels with "".
(4) Lastly, join the array back into a string.
Problem: I don't think the code is looping through the array.
Can anyone find a solution?
def abbreviate_sentence(sent):
split_string = sent.split()
for word in split_string:
if len(word) > 4:
abbrev = word.replace("a", "").replace("e", "").replace("i", "").replace("o", "").replace("u", "")
sentence = " ".join(abbrev)
return sentence
print(abbreviate_sentence("follow the yellow brick road")) # => "fllw the yllw brck road"
I just figured out that the "abbrev = words.replace..." line was incomplete.
I changed it to:
abbrev = [words.replace("a", "").replace("e", "").replace("i", "").replace("o", "").replace("u", "") if len(words) > 4 else words for words in split_string]
I found the part of the solution here: Find and replace string values in list.
It is called a List Comprehension.
I also found List Comprehension with If Statement
The new lines of code look like:
def abbreviate_sentence(sent):
split_string = sent.split()
for words in split_string:
abbrev = [words.replace("a", "").replace("e", "").replace("i", "").replace("o", "").replace("u", "")
if len(words) > 4 else words for words in split_string]
sentence = " ".join(abbrev)
return sentence
print(abbreviate_sentence("follow the yellow brick road")) # => "fllw the yllw brck road"

Most efficient way to format a string in scala with leading Zero's and a comma instead of a decimal point

Trying to create a simple function whereby a String value is passed in i.e. "1" and the formatter should return the value with leading zeros and 5 decimal points however instead of a dot '.' I'm trying to return it with a comma ','
This is what I have attempted however its not working because the decimalFormatter can only handle numbers and not a string. The end goal is to get from "1" to "000000001,00000" - character length is 14 in total. 5 0's after the comma and the remaining before the comma should be padded out 0's to fill the 9 digit requirement.
Another example would be going from "913" to "000000913,00000"
def numberFormatter (value: String): String =
{
import java.text.DecimalFormat
val decimalFormat = new DecimalFormat("%09d,00000")
val formattedValue = decimalFormat.format(value)
return formattedValue
}
Any help would be much appreciated. Thank you in advance.
It's easy enough to format a String padded with spaces, but with zeros not so much. Still, it's not so hard to roll your own.
def numberFormatter(value :String) :String =
("0" * (9 - value.length)) + value + ",00000"
numberFormatter("1") //res0: String = 000000001,00000
numberFormatter("913") //res1: String = 000000913,00000
Note that this won't truncate the input String. So if value is longer than 9 characters then the result will be longer than the desired 15 characters.
def f(value:String) = new java.text.DecimalFormat("000000000.00000").format(value.toFloat).replace(".", ",")
scala> f(913f)
res5: String = 000000913,00000
// Edit: Use .toFloat (or .toInt, .toLong, etc.) to convert your string to a number first.

Split: A subscript must be between 1 and the size of the array

I have a super simple formula. The problem is that sometimes the data doesn't have a second value, or sometimes the value is blank.
Split ({PO_RECEIVE.VENDOR_LOT_ID}," ")[2]
ID
111 222
123
123 222
I was thinking if I could come up with some logic to figure out whether the string has multiple value's it would solve my problem, but haven't quiet found what I'm looking for:
If {PO_RECEIVE.VENDOR_LOT_ID} = SingleOrBlankString then
{PO_RECEIVE.VENDOR_LOT_ID} else
Split ({PO_RECEIVE.VENDOR_LOT_ID}," ")[2]
Better Example Data:
3011111*42011111111
2711 00291111111
711111//12111111111
/J1111 69111111111
170111
If the string can contain a maximum of two values, separated by a space, then you can check if the string contains a space using the InStr function:
If InStr({PO_RECEIVE.VENDOR_LOT_ID}, " ") > 0 Then
{PO_RECEIVE.VENDOR_LOT_ID}
Else
Split ({PO_RECEIVE.VENDOR_LOT_ID}," ")[2]
If there can be multiple spaces between the parts you can use following formulas to get the values:
Left part:
This function returns the left part of the string until the first space.
If InStr({PO_RECEIVE.VENDOR_LOT_ID}, " ") > 0 Then
Left({PO_RECEIVE.VENDOR_LOT_ID}, InStr({PO_RECEIVE.VENDOR_LOT_ID}, " "))
Right part:
This function returns the right part of the string after the last space.
The InStrRev-function returns the position of the last space because it searches the string backwards.
The Len-function returns the length of the string.
[length] - [position of last space] = [length of the right part]
If InStr({PO_RECEIVE.VENDOR_LOT_ID}, " ") > 0 Then
Right({PO_RECEIVE.VENDOR_LOT_ID}, Len({PO_RECEIVE.VENDOR_LOT_ID}) - InStrRev(testString, " "))