Creating string from Custom label - string

I have a custom label in CSV format called Test having value abc, xyz and I want to create a string in the form 'abc','xyz'. How would we do that?
Code Written so far
String str = System.Label.Test;
// next steps

The code below splits the label at the comma to create a list. Each string in the list is them trimmed to remove whitespace and added to another list. That list is joined using ',' as the delimiter.
String str = System.Label.Test; // next steps
final String SINGLE_QUOTE = '\'';
final String COMMA = ',';
final String DELIMITER = SINGLE_QUOTE + COMMA + SINGLE_QUOTE;
String formattedLabel = SINGLE_QUOTE;
List<String> stringItems = new List<String>();
for(String item : str.split(',')){
stringItems.add(item.trim());
}
formattedLabel += String.join(stringItems, DELIMITER);
formmatedLabel += SINGLE_QUOTE;
System.debug(formattedLabel);

Related

Remove \n from a given strings in dart

Given a string
String item = 'Hello\nWorld'
I want a function that removes \n from the given string
Something like this
String newItem = removeBacklash(item);
print(newItem); // newItem will be 'HelloWorld'
You can use .replaceAll()
String removeBacklash(String data) => data.replaceAll("\n", "");
More about String

How to parse a string for numbers and words (strings)? IN JAVA

I have a list of strings that contain names and a series of numbers, ex: "John James Hollywood 1 2 3".
I want to parse out the names using a loop so that I can scan each part of the text and store it in a new string variable. Then I tried to use a conditional to put any numbers in a different variable. I will calculate the average of the numbers and then print the name and the average of the numbers, so the output looks like this:
John James Hollywood: 2
I don't know whether to use the input string stream or what my first steps are.
public static String parseNameNumbers(String text)
{
Scanner scnr = new Scanner(text);
Scanner inSS = null;
int numSum = 0;
int countNum = 0;
String newText = new String();
String sep = "";
while(scnr.hasNext()) {
if(scnr.hasNextInt()) {
numSum = numSum + scnr.nextInt();
countNum++;
}
else {
newText = newText + sep + scnr.next();
sep = " ";
}
}
return newText + ": " + (numSum/countNum);
}

When trying to add a space back into a tokenized String, why are two spaces added into my output?

Beginner here. Sorry for the vague title but the code should put my question into perspective.
public static void main(String [] args)
{
String sentence = "hi. how are you! i'm just dandy.";
String tokenSent;
tokenSent = sentenceCapitalizer(sentence);
System.out.println(tokenSent);
}
public static String sentenceCapitalizer(String theSentence)
{
StringTokenizer strTokenizer = new StringTokenizer(theSentence, ".!", true);
String token = null;
String totalToken = "";
String ch = "";
while(strTokenizer.hasMoreTokens())
{
token = strTokenizer.nextToken().trim();
token = token.replace(token.charAt(0), Character.toUpperCase(token.charAt(0)));
StringBuilder str = new StringBuilder(token);
str.append(" ");
totalToken += str;
}
return totalToken;
}
OUTPUT AS IS: Hi . How are you ! I'm just dandy .
I was able to capitalize the first letter of each sentence but I'm wanting the output to keep the same format as the original String. The problem is that it puts a space before and after the ending punctuation. Is there any way to fix this problem using only a StringBuilder and/or StringTokenizer? Thank you for your time.
You can do this.
String delim = ".!";
StringTokenizer strTokenizer = new StringTokenizer(theSentence, delim, true);
Then,
if(delim.contains(token))
str.append(" ");
When you declare new StringTokenizer(theSentence, ".!", true) ,true enables to return delimiter characters also as next token. Since you appending a space to each token, it is normal to get that output.
You can check if the token is a delimiter character, if so, you add the space.
if(token.length() == 1 &&
delims.indexOf(token.charAt(0)) != -1)
str.append(" "); //we just hit the delim char, add space

How to cut a set of characters in a stringbuilder?

Is there a way to split a set of characters in a stringbuilder - for example if I have "One Two Three Four" is there a way of getting each individual word which I could then put into a list (using a foreach loop)
I think there is no direct way to split StringBuilder to an array, you need convert to String first, like this:
StringBuilder sb = new StringBuilder();
sb.append("One Two Three Four");
String[] myArray = sb.toString().split(" ");
for java
StringBuilder sb = new StringBuilder("One Two Three Four");
String[] words = sb.toString().split("[\\s]+");
please try using the below mentioned code. Hope it helps you in your task :
//Create Stringbuilder
StringBuilder strb = new StringBuilder();
strb.Append("One Two Three Four");
// Convert Stringbuilder to string
string a = strb.ToString();
// Split the string based on seprator. Here seprator is space " "
string[] str = a.Split();
List<string> al1 = new List<string>();
// Add the word of splited string to a string List
foreach (string str1 in str)
{
al1.Add(str1);
}

How to Convert an ArrayList to string C#

ArrayList arr = new ArrayList();
string abc =
What should I do to convert arraylist to a string such as abc = arr;Updated QuestOther consideration from which i can complete my work is concatination of string(need help in that manner ). suppose i have a string s="abcdefghi.."by applying foreach loop on it and getting char by matching some condition and concatinating every char value in some insatnce variable of string type i.e string subString=+;Something like thisstring tem = string.Empty;
string temp =string.Empty;
temp = string.Concat(tem,temp);
Using a little linq and making the assumption that your ArrayList contains string types:
using System.Linq;
var strings = new ArrayList().Cast<string>().ToArray();
var theString = string.Join(" ", strings);
Further reading:
http://msdn.microsoft.com/en-us/library/57a79xd0.aspx
For converting other types to string:
var strings = from object o in myArrayList
select o.ToString();
var theString = string.Join(" ", strings.ToArray());
The first argument to the Join method is the separator, I chose whitespace. It sounds like your chars should all contribute without a separator, so use "" or string.Empty instead.
Update: if you want to concatenate a small number of strings, the += operator will suffice:
var myString = "a";
myString += "b"; // Will equal "ab";
However, if you are planning on concatenating an indeterminate number of strings in a tight loop, use the StringBuilder:
using System.Text;
var sb = new StringBuilder();
for (int i = 0; i < 10; i++)
{
sb.Append("a");
}
var myString = sb.ToString();
This avoids the cost of lots of string creations due to the immutability of strings.
Look into string.Join(), the opposite of string.Split()
You'll also need to convert your arr to string[], I guess that ToArray() will help you do that.
Personally and for memory preservation I’ll do for a concatenation:
System.Collections.ArrayList Collect = new System.Collections.ArrayList();
string temporary = string.Empty;
Collect.Add("Entry1");
Collect.Add("Entry2");
Collect.Add("Entry3");
foreach (String var in Collect)
{
temporary = temporary + var.ToString();
}
textBox1.Text = temporary;

Resources