How can I generate a string of fixed length based on the input given in c#.
For example :
string s="1";
string newstring ;
I want the newstring to be "AB_000001";
// Similarly if
s="xyz";
//I want the newstring as
newstring = "AB_000xyz";
String s = "AB_000000";
String newString="xyz";
s = s.Remove(s.Length - newString.Length, newString.Length);
s = s + newString;
string basestr = "AB_000000";
string inp = "xyz";
string res = basestr.Substring(0, basestr.Length - inp.Length) + inp;
Related
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);
How can I reverse the string which includes numbers and letters, but I want to output the letters in reverse order. (Without using string functions)
string = 'Hellow 432'
length = len(string)
output = ""
while length>0:
output += string[length-1]
length = length-1
print (output)
String details = "Hellow 432";
string numeric = "";
string nonnumeric = "";
char[] mychar = details.ToCharArray();
foreach (char ch in mychar)
{
if (char.IsDigit(ch))
{
numeric = numeric + ch.ToString();
}
else
{
nonnumeric =ch.ToString()+ nonnumeric;
}
}
return nonnumeric + numeric;
}
String xStr = "hello/master/yoda";
String remv_last = xStr.substring(0, xStr.lastIndexOf("/"))
System.out.println(remv_last);
output
hello/master
My question is; how can I get this output, thanks for helping.
master/yoda
use indexOf
String xStr = "hello/master/yoda";
String remv_last = xStr.substring(xStr.indexOf("/") + 1);
System.out.println(remv_last);
You can use simple indexOf it will give you the first occurrence only
String xStr = "hello/master/yoda";
String remv_last = xStr.substring(0, xStr.lastIndexOf("/"));
int firstIndex = xStr.indexOf("/");
String firstCharacter = xStr.substring(firstIndex,xStr.length());
System.out.println(remv_last);
System.out.println(firstCharacter);
It will print required
Input: String str="Fund testing testing";
Output: str="Fund";
After fund whatever the text is there need to remove that text.
Please suggest some solution.
The easiest way to solve this is a .Substring() method, as you can provide it the start index of your original string and length of the string you need:
var length = "Fund".Length;
var str = "Fund testing testing";
Console.WriteLine(str.Substring(0, length)); //returns "Fund"
var str1 = "testFund testing testing";
Console.WriteLine(str1.Substring(4, length)); //returns "Fund"
var str2 = "testFund testing testing";
Console.WriteLine(str2.Substring(str2.IndexOf("Fund"), length)); //returns "Fund"
You can also use regular expression like this:
string strRegex = #".*?(Fund).*";
Regex myRegex = new Regex(strRegex, RegexOptions.Singleline);
string strTargetString = #"Fund testing testing";
string strReplace = #"$1";
return myRegex.Replace(strTargetString, strReplace);
As mentioned in comments below, replace can lack performance and is kind of overkill, so regex Match can be better. Here is how it looks like:
string strRegex = #".*?(Fund).*";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = "\n\n" + #" Fund testing testing";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
var fund = myMatch.Groups[1].Value;
Console.WriteLine(fund);
}
}
Note that Groups first element is your entire match
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;