Is there like "firstIndexOf" code? - string

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

Related

how to make single string of all key and values of map using lambda expression or stream

Map attributeMap = new TreeMap<>();
attributeMap.put("C","FIRSTNAMe");
attributeMap.put("C2","LASTNAMe");
attributeMap.put("C3","1111");
attributeMap.put("C4","ABCNAMe");
How to make single String of above example
output is c,c2,c3,c4 and FIRSTNAMe,LASTNAMe,'1111',ABCNAMe
Something like this:
Predicate<String> predicate = "1111"::equals;
String left = attributeMap.keySet().stream().map(String::toLowerCase).collect(Collectors.joining(","));
String right = attributeMap.values().stream().map(x -> (predicate.test(x) ? "'" + x + "'" : x)).collect(Collectors.joining(","));
System.out.println(left + right);

Adding a space in a String after a certain character(VB Code)

Hello everyone.
Dim txt1 As Double = Convert.ToDouble(TextBox1.Text) / 100
Dim txt2 As Double = Convert.ToDouble(TextBox2.Text)
Dim txt3 As Double = Convert.ToDouble(TextBox3.Text)
Dim txtResult As Double = Convert.ToDouble(TextBox4.Text)
Dim result As Double = txt1 * txt2 * txt3
TextBox4.Text = result
As you can see I get my result depending on what the user types in. So I have to add a space after a certain character. Textbox14.text(0) <--- after this do I want my space. It's so that after the value is higher than 999 it should type out 1 000 and not 1000. Thank you very much for any useful help, I've truly looked everywhere, I just can't find anything.
You talking about group separator. Custom Numeric Format Strings
You can use .ToString() method and define group separator in the format.
TextBox4.Text = result.ToString("0,0.000")
Different separators will be used based on the local system's language/region settings.
You can define your custome separator manually
var cultureInfo = new System.Globalization.CultureInfo("en-US");
var numberInfo = cultureInfo.NumberFormat;
numberInfo.NumberGroupSeparator = " ";
TextBox4.Text = result.ToString("0,0.000", numberInfo)
If I get it right, you want every 3 chars a space, right ?
Like 1 000 000 ?
try this :
Dim result As String, str As String, ret As String
Dim i As Integer
Dim arr As Char()
'your text to space
result = "10000000"
'reverte so we start with the end
result = StrReverse(result)
i = 0
ret = ""
' make a char array which each char is an own array element
arr = result.Take(result.Length).ToArray
'iterate through all elements
For Each str In arr
' skip the first element .
' only add a space every 3 elements
If (i <> 0) And (i Mod 3 = 0) Then
ret = ret + " "
End If
ret = ret + str
i = i + 1
Next
' revers again the output
ret = StrReverse(ret)
MsgBox(ret)

Remove some text from a string after some constant value(string)

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

How can i replace space with _ (underscore) in a string?

Suppose initial string is
String result= "Welcome To All" for that i need updated result as
String result="Welcome_To_All" string value is dynamic,how can i do that,thank for help?
I believe this is what you're looking for
string oldstr = "Hello World";
string s = oldstr.Replace(" ", "_");
(s == "Hello_World");
I'm going to guess that you're using javascript and you're ok with using regular expressions.
result = result.replace(/ /g, "_");

Generate string of fixed length in C#

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;

Resources