Click here to Skip to main content
15,886,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string and I want to cut part of the string and save it in another string and the first string should be saved but without the part that supposed to be cut.
the string could be changeable but I want to get the concept.
for example, if I have:

String str1 = "hello world cout<<"hi,this is Sunday"<<endl;

I want to cut this part:
cout<<"hi,this is Sunday"<<endl;


and save it in different String and the result should be:

String str1 = "hello world";

and the new String should have the cut content.

String str2 = cout<<"hi,this is Sunday"<<endl;


What I have tried:

I tried to use replace all with this regex

String Str2 = Str1.replaceAll("cout<<.*\n?;", "");

but here I only remove the part that I want to be saved completely, and cannot find a way to save it in another string.



Thank you in advance
Posted
Updated 21-Oct-20 4:05am
Comments
[no name] 20-Oct-20 14:37pm    
if you had used <<...>>, instead of << ... <<, it would have made things easier.

 
Share this answer
 
Comments
HishamMohammedA 20-Oct-20 16:16pm    
the string is not constant which means it will be different each time, so I don't know the index or substring of it because it can be different each time.
Richard MacCutchan 20-Oct-20 16:22pm    
That is why you should use indexOf.
HishamMohammedA 20-Oct-20 17:35pm    
can you provide an example
Richard MacCutchan 21-Oct-20 3:12am    
Look at the documentation, it clearly explains how to find the index of a string. Once you have that number then extracting the two substrings is a simple matter. If you aspire to become a developer then you need to try things. The more you practice the better your skills.
HishamMohammedA 21-Oct-20 9:14am    
I knew how to use IndexOf to find the index number of the text, but in that case, how do I save it the text that I extracted in another String?
Java
String str1 = "hello world cout<<\"hi,this is Sunday\"<<endl";
int index = str1.indexOf("cout");
System.out.printf("index: %d\n", index);
String part1 = str1.substring(0, index);
String part2 = str1.substring(index);
System.out.printf("part1: %s\n", part1);
System.out.printf("part2: %s\n", part2);
 
Share this answer
 
Comments
HishamMohammedA 21-Oct-20 12:09pm    
Thank you for your replay @Richard, but my main issue is that as I mention my text will be different each time and I am only looking for the text that I want to extract by using regex as a parameter but I believe the indexOf can't receive regex as a parameter.
HishamMohammedA 21-Oct-20 12:18pm    
for example, if I have a String like this, String str ="if(i=1)cout<
HishamMohammedA 21-Oct-20 12:21pm    
sorry for separating the two comments but it refuses to put the full string.
In indexOf, I can put the letter "c" and it will cut what comes after but as you can see in this case I already know the example but what if cout is changed then I can't rely on the letter "c" so I want to use if condition regex so it will cut what comes after it and save it in a new string
Richard MacCutchan 21-Oct-20 12:36pm    
The issue is the same whichever method you use. You need to know where to split the string. If it is at 'cout', or 'print', then you need that information before you can do it. And a regex will not help you if the search words are different.
HishamMohammedA 21-Oct-20 13:30pm    
I tired regex but you are right I must know the part that I have to handle it, so maybe I will ask the user to put a mark or something before the part I want to take to make it easier since the string will change each time.
Thank you Richard for helping me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900